library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine

First Dataset, 4/10/24

Analyze dataset from 4/10/24, for the gels made on 4/8/24. There is an n = 3 for each thickness, with 3 gels of 200 um thickness (“thin”) and 3 gels of 1000 um thickness (“thick”). Three profiles were taken per gel, at a light intensity of PAR” 90 umol/m^2/s and a flow rate of 0.5 cm/s. Three additional profiles were taken per gel in the dark after 20 minutes of dark adaptation. The profiles were taken in the middle of the hydrogel, in the same position for all 6 profiles per gel.

Plotting profiles

files <- list.files(path = "./Polymerized040824_Measured041024", pattern = "\\.csv$", full.names = TRUE)

plot_list <- list()

for (file in files) {
  file_name <- tools::file_path_sans_ext(basename(file))
  
  file_data <- read_csv(paste(file),show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`)

  plot <- ggplot(data = file_data, aes(y = `Oxygen (µmol/L)`, x = `Depth (µm)`,
                                       color = `Condition`, shape = `Replicate`)) + 
      geom_point() + geom_line() +
      coord_flip() + scale_x_reverse() + ylim(190, 295) + 
      theme_bw() + ggtitle(file_name) +
      annotate("text", x = -1900, y = 295, label = paste("Start:", file_data$Time[1]), vjust = 1, hjust = 1, size = 3) +
      annotate("text", x = -1800, y = 295, label = paste("End:", file_data$Time[nrow(file_data)]), vjust = 1, hjust = 1, size = 3) +
    theme(legend.position = "bottom") + scale_color_manual(values = c("#4169E1", "#7EC8E3")) + theme_bw()
  
  #print(plot)
  
  plot_list[[file_name]] <- plot
}
## New names:
## New names:
## New names:
## New names:
## New names:
## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`
arranged_plots <- arrangeGrob(grobs = plot_list, ncol = 3)

gridExtra::grid.arrange(arranged_plots)

ggsave("Plots/Polymerized040824_Measured041024_plots.png", arranged_plots, width = 16, height = 9, units = "in", device = "png")

Notes about calculating flux

Wangpraseurt et al 2022: For each profile, the diffusive O2 flux was calculated using Fick’s first law of diffusion as described previously[31] (using a diffusion coefficient of DO2 = 2.2417 × 10−5). The effective DBL thickness δe as the intersection between the extrapolation of the linear part of the O2 slope within the boundary layer that intercepts with the O2 concentration of the bulk medium was calculated.[75]

Wangpraseurt et al 2020: “The diffusive O2 flux was calculated via Fick’s first law of diffusion for a water temperature = 25 °C and salinity = 30 using a molecular diffusion coefficient for O2 = 2.255 × 10−5 cm2 s−1(2). Gross photosynthesis was estimated via the light-dark shift method36. A flow chamber set-up provided slow laminar flow (flow rate = 0.5 cm s−1) and a fiber-optic halogen lamp (Schott KL2500, Schott, Germany) provided white light at defined levels of incident irradiance (400–700 nm) (0, 110, 220, and 1200 µmol photons m−2 s−1)2. Photosynthesis-irradiance curves were fitted to an exponential function37.”

Using segmented linear regression, segmented R package

library(segmented)
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
results_df_1000um_segmented <- data.frame(
  File = character(),
  Scaffold_Thickness = character(),
  Replicate = character(),
  slope = numeric(),
  Flux = numeric(),
  Breakpoint = numeric(),
  Oxygen_Surface = numeric(),
  Oxygen_breakpoint = numeric(),
  stringsAsFactors = FALSE
)

# Function to perform segmented regression and extract slope after breakpoint
perform_segmented_regression <- function(oxygen_concentration, depth) {
  # Fit segmented regression model
  fit <- segmented(lm(oxygen_concentration ~ depth), seg.Z = ~depth, psi = -250)
  
  # Extract the breakpoint(s)
  breakpoint <- fit$psi[, 2]
  
  # Find the index of the datapoint nearest to the breakpoint
  breakpoint_index <- which.min(abs(depth - breakpoint))

  # Extract the oxygen concentration at the breakpoint
  Oxygen_breakpoint <- oxygen_concentration[breakpoint_index]
  
  # Extract the oxygen concentration at depth = 0
  Oxygen_Surface <- oxygen_concentration[which(depth == 0)]
  
  # Extract the slope after the breakpoint for each segment
  lm_fit <- lm(oxygen_concentration ~ depth,
                 data = data.frame(depth = depth[breakpoint_index:length(depth)],
                                   oxygen_concentration = oxygen_concentration[breakpoint_index:length(oxygen_concentration)]))
  slope <- coef(lm_fit)["depth"]

  # Calculate fitted values for segmented regression lines
  fitted_values <- fitted(fit, newdata = data.frame(depth = depth))
  
  # Create dataframe with depth and fitted oxygen concentration values
  fitted_data <- data.frame("Depth" = depth, "Oxygen" = fitted_values)
  
  return(list(
    slope = slope,
    breakpoint = breakpoint,
    Oxygen_breakpoint = Oxygen_breakpoint,
    Oxygen_Surface = Oxygen_Surface,
    fitted_data = fitted_data
  ))
}

# Get list of CSV file names with "1000" in the title
file_list <- list.files(path = "./Polymerized040824_Measured041024", pattern = "1000.*\\.csv$", full.names = TRUE)

# Loop over files
for (file_path in file_list) {
  # Read data from file
  data <- read_csv(file_path, show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>%
    arrange(`Depth (µm)`)
  
  # Loop over replicates
  for (replicate_id in unique(data$Replicate)) {
    # Subset data for the current replicate
    replicate_data <- data %>%
      filter(Replicate == replicate_id & Condition == "Light")
    
    # Extract oxygen concentration and depth
    oxygen_concentration <- replicate_data %>% pull(`Oxygen (µmol/L)`)
    depth <- replicate_data %>% pull(`Depth (µm)`)
    
    # Perform segmented regression
    regression_results <- perform_segmented_regression(oxygen_concentration, depth)
    
    # Plot data and segmented regression lines
    p <- ggplot(replicate_data, aes(x = `Depth (µm)`, y = `Oxygen (µmol/L)`)) +
      geom_point() +
      geom_line(data = regression_results$fitted_data, aes(x = `Depth`, y = `Oxygen`), color = "red") +
      labs(title = paste("Replicate", replicate_id), x = "Depth (µm)", y = "Oxygen (µmol/L)")
    
    print(p)
    
    # Calculate flux using Fick’s first law of diffusion
    DO2 <- 2.2417e-5  # Diffusion coefficient of oxygen
    flux <- DO2 * regression_results$slope
        
    # Store results in dataframe
    results_df_1000um_segmented <- bind_rows(results_df_1000um_segmented, data.frame(
      File = basename(file_path),
      Scaffold_Thickness = "1000 um",
      Replicate = replicate_id,
      slope = regression_results$slope,
      Flux = flux,  
      Breakpoint = regression_results$breakpoint,  
      Oxygen_Surface = regression_results$Oxygen_Surface, 
      Oxygen_breakpoint = regression_results$Oxygen_breakpoint,
      stringsAsFactors = FALSE
    ))
  }
}
## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

# Print the results dataframe
print(results_df_1000um_segmented)
##                             File Scaffold_Thickness Replicate     slope
## depth...1 Gel040824_1000um_4.csv            1000 um         1 0.4163000
## depth...2 Gel040824_1000um_4.csv            1000 um         2 0.2972034
## depth...3 Gel040824_1000um_4.csv            1000 um         3 0.3480109
## depth...4 Gel040824_1000um_5.csv            1000 um         1 0.3158300
## depth...5 Gel040824_1000um_5.csv            1000 um         2 0.2252707
## depth...6 Gel040824_1000um_5.csv            1000 um         3 0.3405020
## depth...7 Gel040824_1000um_6.csv            1000 um         1 0.3064920
## depth...8 Gel040824_1000um_6.csv            1000 um         2 0.2625240
## depth...9 Gel040824_1000um_6.csv            1000 um         3 0.2755740
##                   Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1 9.332197e-06  -186.4981        293.493           214.610
## depth...2 6.662409e-06  -228.7611        285.615           212.969
## depth...3 7.801359e-06  -226.2641        292.447           209.878
## depth...4 7.079961e-06  -207.0086        281.456           221.030
## depth...5 5.049894e-06  -276.5534        282.833           215.546
## depth...6 7.633033e-06  -176.8002        283.175           216.541
## depth...7 6.870631e-06  -162.7789        264.643           219.004
## depth...8 5.885001e-06  -178.6104        269.245           217.059
## depth...9 6.177542e-06  -207.1288        272.468           218.221
results_df_200um_segmented <- data.frame(
  File = character(),
  Scaffold_Thickness = character(),
  Replicate = character(),
  slope = numeric(),
  Flux = numeric(),
  Breakpoint = numeric(),
  Oxygen_Surface = numeric(),
  Oxygen_breakpoint = numeric(),
  stringsAsFactors = FALSE
)

# Function to perform segmented regression and extract slope after breakpoint
perform_segmented_regression <- function(oxygen_concentration, depth) {
  # Fit segmented regression model
  fit <- segmented(lm(oxygen_concentration ~ depth), seg.Z = ~depth, psi = -400)
  
  # Extract the breakpoint(s)
  breakpoint <- fit$psi[, 2]
  
  # Find the index of the datapoint nearest to the breakpoint
  breakpoint_index <- which.min(abs(depth - breakpoint))

  # Extract the oxygen concentration at the breakpoint
  Oxygen_breakpoint <- oxygen_concentration[breakpoint_index]
  
  # Extract the oxygen concentration at depth = 0
  Oxygen_Surface <- oxygen_concentration[which(depth == 0)]
  
  # Extract the slope after the breakpoint for each segment
  lm_fit <- lm(oxygen_concentration ~ depth,
                 data = data.frame(depth = depth[breakpoint_index:length(depth)],
                                   oxygen_concentration = oxygen_concentration[breakpoint_index:length(oxygen_concentration)]))
  slope <- coef(lm_fit)["depth"]
  
  # Calculate fitted values for segmented regression lines
  fitted_values <- fitted(fit, newdata = data.frame(depth = depth))
  
  # Create dataframe with depth and fitted oxygen concentration values
  fitted_data <- data.frame("Depth" = depth, "Oxygen" = fitted_values)
  
  return(list(
    slope = slope,
    breakpoint = breakpoint,
    Oxygen_breakpoint = Oxygen_breakpoint,
    Oxygen_Surface = Oxygen_Surface,
    fitted_data = fitted_data
  ))
}

# Get list of CSV file names with "200" in the title
file_list <- list.files(path = "./Polymerized040824_Measured041024", pattern = "200.*\\.csv$", full.names = TRUE)

# Loop over files
for (file_path in file_list) {
  # Read data from file
  data <- read_csv(file_path, show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>%
    arrange(`Depth (µm)`)
  
  # Loop over replicates
  for (replicate_id in unique(data$Replicate)) {
    # Subset data for the current replicate
    replicate_data <- data %>%
      filter(Replicate == replicate_id & Condition == "Light")
    
    # Extract oxygen concentration and depth
    oxygen_concentration <- replicate_data %>% pull(`Oxygen (µmol/L)`)
    depth <- replicate_data %>% pull(`Depth (µm)`)
    
    # Perform segmented regression
    regression_results <- perform_segmented_regression(oxygen_concentration, depth)
    
    # Plot data and segmented regression lines
    p <- ggplot(replicate_data, aes(x = `Depth (µm)`, y = `Oxygen (µmol/L)`)) +
      geom_point() +
      geom_line(data = regression_results$fitted_data, aes(x = `Depth`, y = `Oxygen`), color = "red") +
      labs(title = paste("Replicate", replicate_id), x = "Depth (µm)", y = "Oxygen (µmol/L)")
    
    print(p)
    
    # Calculate flux using Fick’s first law of diffusion
    DO2 <- 2.2417e-5  # Diffusion coefficient of oxygen
    flux <- DO2 * regression_results$slope
        
    # Store results in dataframe
    results_df_200um_segmented <- bind_rows(results_df_200um_segmented, data.frame(
      File = basename(file_path),
      Scaffold_Thickness = "200 um",
      Replicate = replicate_id,
      slope = regression_results$slope,
      Flux = flux,  
      Breakpoint = regression_results$breakpoint,  
      Oxygen_Surface = regression_results$Oxygen_Surface, 
      Oxygen_breakpoint = regression_results$Oxygen_breakpoint,
      stringsAsFactors = FALSE
    ))
  }
}
## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

# Print the results dataframe
print(results_df_200um_segmented)
##                            File Scaffold_Thickness Replicate      slope
## depth...1 Gel040824_200um_4.csv             200 um         1 0.10182714
## depth...2 Gel040824_200um_4.csv             200 um         2 0.12417314
## depth...3 Gel040824_200um_4.csv             200 um         3 0.11439429
## depth...4 Gel040824_200um_5.csv             200 um         1 0.09876400
## depth...5 Gel040824_200um_5.csv             200 um         2 0.09298171
## depth...6 Gel040824_200um_5.csv             200 um         3 0.10329143
## depth...7 Gel040824_200um_6.csv             200 um         1 0.09034171
## depth...8 Gel040824_200um_6.csv             200 um         2 0.06573738
## depth...9 Gel040824_200um_6.csv             200 um         3 0.08116286
##                   Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1 2.282659e-06  -278.3708        235.138           206.914
## depth...2 2.783589e-06  -269.4554        237.367           208.720
## depth...3 2.564377e-06  -269.4545        235.725           208.342
## depth...4 2.213993e-06  -236.2531        230.437           206.340
## depth...5 2.084371e-06  -237.5620        229.684           207.066
## depth...6 2.315484e-06  -266.7288        232.367           207.491
## depth...7 2.025190e-06  -274.4906        231.559           209.808
## depth...8 1.473635e-06  -340.0699        229.721           207.371
## depth...9 1.819428e-06  -310.8676        230.814           207.816

Stats

Full_results_segmented_041024 <- rbind(results_df_1000um_segmented, results_df_200um_segmented)

Full_results_segmented_041024
##                              File Scaffold_Thickness Replicate      slope
## depth...1  Gel040824_1000um_4.csv            1000 um         1 0.41630000
## depth...2  Gel040824_1000um_4.csv            1000 um         2 0.29720343
## depth...3  Gel040824_1000um_4.csv            1000 um         3 0.34801086
## depth...4  Gel040824_1000um_5.csv            1000 um         1 0.31583000
## depth...5  Gel040824_1000um_5.csv            1000 um         2 0.22527071
## depth...6  Gel040824_1000um_5.csv            1000 um         3 0.34050200
## depth...7  Gel040824_1000um_6.csv            1000 um         1 0.30649200
## depth...8  Gel040824_1000um_6.csv            1000 um         2 0.26252400
## depth...9  Gel040824_1000um_6.csv            1000 um         3 0.27557400
## depth...11  Gel040824_200um_4.csv             200 um         1 0.10182714
## depth...21  Gel040824_200um_4.csv             200 um         2 0.12417314
## depth...31  Gel040824_200um_4.csv             200 um         3 0.11439429
## depth...41  Gel040824_200um_5.csv             200 um         1 0.09876400
## depth...51  Gel040824_200um_5.csv             200 um         2 0.09298171
## depth...61  Gel040824_200um_5.csv             200 um         3 0.10329143
## depth...71  Gel040824_200um_6.csv             200 um         1 0.09034171
## depth...81  Gel040824_200um_6.csv             200 um         2 0.06573738
## depth...91  Gel040824_200um_6.csv             200 um         3 0.08116286
##                    Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1  9.332197e-06  -186.4981        293.493           214.610
## depth...2  6.662409e-06  -228.7611        285.615           212.969
## depth...3  7.801359e-06  -226.2641        292.447           209.878
## depth...4  7.079961e-06  -207.0086        281.456           221.030
## depth...5  5.049894e-06  -276.5534        282.833           215.546
## depth...6  7.633033e-06  -176.8002        283.175           216.541
## depth...7  6.870631e-06  -162.7789        264.643           219.004
## depth...8  5.885001e-06  -178.6104        269.245           217.059
## depth...9  6.177542e-06  -207.1288        272.468           218.221
## depth...11 2.282659e-06  -278.3708        235.138           206.914
## depth...21 2.783589e-06  -269.4554        237.367           208.720
## depth...31 2.564377e-06  -269.4545        235.725           208.342
## depth...41 2.213993e-06  -236.2531        230.437           206.340
## depth...51 2.084371e-06  -237.5620        229.684           207.066
## depth...61 2.315484e-06  -266.7288        232.367           207.491
## depth...71 2.025190e-06  -274.4906        231.559           209.808
## depth...81 1.473635e-06  -340.0699        229.721           207.371
## depth...91 1.819428e-06  -310.8676        230.814           207.816
mean_flux <- Full_results_segmented_041024 %>%
  group_by(Scaffold_Thickness) %>%
  summarize(mean_flux = mean(Flux),
            min = min(Flux))

# Running ANOVA
anova_result <- aov(Flux ~ Scaffold_Thickness, data = Full_results_segmented_041024)
summary(anova_result)
##                    Df    Sum Sq   Mean Sq F value  Pr(>F)    
## Scaffold_Thickness  1 1.024e-10 1.024e-10   121.2 7.1e-09 ***
## Residuals          16 1.352e-11 8.400e-13                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# What about for oxygen surface concentration?

mean_Oxygen_Surface <- Full_results_segmented_041024 %>%
  group_by(Scaffold_Thickness) %>%
  summarize(mean_Oxygen_Surface = mean(Oxygen_Surface),
            min = min(Oxygen_Surface))

# Running ANOVA
anova_result <- aov(Oxygen_Surface ~ Scaffold_Thickness, data = Full_results_segmented_041024)
summary(anova_result)
##                    Df Sum Sq Mean Sq F value   Pr(>F)    
## Scaffold_Thickness  1  10395   10395   193.7 2.33e-10 ***
## Residuals          16    858      54                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# What about DBL Thickness?

mean_DBL_end <- Full_results_segmented_041024 %>%
  group_by(Scaffold_Thickness) %>%
  summarize(mean_DBL_end = mean(Breakpoint),
            max = max(Breakpoint))

# Running ANOVA
anova_result <- aov(Breakpoint ~ Scaffold_Thickness, data = Full_results_segmented_041024)
summary(anova_result)
##                    Df Sum Sq Mean Sq F value   Pr(>F)    
## Scaffold_Thickness  1  22250   22250   19.44 0.000439 ***
## Residuals          16  18316    1145                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Combined plots

library(cowplot)

# Plotting the flux values for each thickness
flux_plot <- ggplot(Full_results_segmented_041024, aes(x = Scaffold_Thickness, y = Flux)) +
  geom_boxplot() + geom_point(aes(color = File), show.legend = FALSE, position = position_jitter(width = 0.05)) + 
  geom_text(data = mean_flux, aes(label = paste("Mean Flux =\n", sprintf("%.2e", mean_flux)),
                                  y = min-.000001), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Thickness (µm)", y = "Flux") + theme_bw()

# Plotting the surface O2 values for each thickness
oxygen_surface_plot <- ggplot(Full_results_segmented_041024, aes(x = Scaffold_Thickness, y = Oxygen_Surface)) +
  geom_boxplot() + geom_point(aes(color = File), show.legend = FALSE, position = position_jitter(width = 0.05)) + 
  geom_text(data = mean_Oxygen_Surface, aes(label = paste("Mean Surface O2 =\n", round(mean_Oxygen_Surface, 1)),
                                            y = min-10), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Thickness (µm)", y = "Oxygen Surface Concentration (µmol/L)") + theme_bw()

# Plotting the DBL Thickness for each scaffold thickness
dbl_thickness_plot <- ggplot(Full_results_segmented_041024, aes(x = Scaffold_Thickness, y = -Breakpoint)) +
  geom_boxplot() + geom_point(aes(color = File), show.legend = FALSE, position = position_jitter(width = 0.05)) + 
  geom_text(data = mean_DBL_end, aes(label = paste("Mean DBL Thickness =\n", round(-mean_DBL_end, 1)),
                                     y = -max-22), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Thickness (µm)", y = "DBL Thickness (µm)") + theme_bw()

# Combine plots with a common legend at the bottom
combined_plot <- plot_grid(flux_plot, oxygen_surface_plot, dbl_thickness_plot, nrow = 1, align = "v", rel_heights = c(1, 1, 1))

# Add overarching title
combined_plot_with_title <- ggdraw() +
  draw_label("Comparison of Flux, Oxygen Surface Concentration, and DBL Thickness\nbetween Hydrogel Thicknesses; Same volumetric density (4/10/24)",
             x = 0.5, y = 0.98, # Adjust position as needed
             hjust = 0.5, vjust = 1,
             size = 12, fontface = "bold")

# Combine title and plots
final_plot <- plot_grid(combined_plot_with_title, combined_plot, ncol = 1, rel_heights = c(0.1, 1))

ggsave("Plots/Polymerized040824_Measured041024_Data_plot.png", final_plot, device = "png", height = 5, width = 12)

#ggsave("Plots/Polymerized040824_Measured041024_Flux_plot.png", flux_plot, device = "png")

Plotting profiles with DBL

files <- list.files(path = "./Polymerized040824_Measured041024", pattern = "\\.csv$", full.names = TRUE)

plot_list <- list()

for (file in files) {
  file_name <- tools::file_path_sans_ext(basename(file))
  
  file_data <- read_csv(paste(file),show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`)
  
  # Extract the DBL depth for the current file
  dbl_depth <- Full_results_segmented_041024$Breakpoint[gsub("\\.csv$", "", Full_results_segmented_041024$File) == file_name]

  plot <- ggplot(data = file_data, aes(y = `Oxygen (µmol/L)`, x = `Depth (µm)`,
                                       color = `Condition`, shape = `Replicate`)) + 
      geom_point() + geom_line() +
      coord_flip() + scale_x_reverse() + ylim(190, 295) + xlim(0, -750) + 
      theme_bw() + ggtitle(file_name) +
      annotate("text", x = -650, y = 295, label = paste("Start:", file_data$Time[1]), vjust = 1, hjust = 1, size = 3) +
      annotate("text", x = -550, y = 295, label = paste("End:", file_data$Time[nrow(file_data)]), vjust = 1, hjust = 1, size = 3) + 
      geom_vline(xintercept = dbl_depth, linetype = "dashed") +  # Add DBL depth line
      theme(legend.position = "bottom") + scale_color_manual(values = c("#4169E1", "#7EC8E3")) + theme_bw()
  
  #print(plot)
  
  plot_list[[file_name]] <- plot
}
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`
arranged_plots <- arrangeGrob(grobs = plot_list, ncol = 3)
## Warning: Removed 144 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 144 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 120 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 120 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 144 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 144 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 144 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 144 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 144 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 144 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 144 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 144 rows containing missing values or values outside the scale range
## (`geom_line()`).
gridExtra::grid.arrange(arranged_plots)

ggsave("Plots/Polymerized040824_Measured041024_plots_DBL.png", arranged_plots, width = 16, height = 9, units = "in", device = "png")

Second Dataset, 4/12/24

Analyze dataset from 4/12/24, for the gels made on 4/10/24. There is an n = 3 for each thickness, with 3 gels of 200 um thickness (“thin”) and 3 gels of 1000 um thickness (“thick”). Three profiles were taken per gel, at a light intensity of PAR 90 umol/m^2/s and a flow rate of 0.5 cm/s. Three additional profiles were taken per gel in the dark after 20 minutes of dark adaptation. The profiles were taken in the middle of the hydrogel, in the same position for all 6 profiles per gel. For this dataset, I began taking profiles into the depth of the gel, which is shown in “positive” depths.

Plotting profiles

files <- list.files(path = "./Polymerized041024_Measured041224", pattern = "\\.csv$", full.names = TRUE)

plot_list <- list()

for (file in files) {
  file_name <- tools::file_path_sans_ext(basename(file))
  
  file_data <- read_csv(paste(file),show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>% filter(Condition == "Light" | Condition == "Dark")
  
  plot <- ggplot(data = file_data, aes(y = `Oxygen (µmol/L)`, x = `Depth (µm)`,
                                       color = `Condition`, shape = `Replicate`)) + 
      geom_point() + geom_line() +
      coord_flip() + scale_x_reverse() + xlim(1000, -1450) + ylim(140, 405) + 
      geom_vline(xintercept = 0, linetype = 3) + 
      theme_bw() + ggtitle(file_name) +
      annotate("text", x = -1400, y = 395, label = paste("Start:", file_data$Time[1]), vjust = 1, hjust = 1, size = 3) +
      annotate("text", x = -1300, y = 395, label = paste("End:", file_data$Time[nrow(file_data)]), vjust = 1, hjust = 1, size = 3) +
    theme(legend.position = "bottom") + scale_color_manual(values = c("#4169E1", "#7EC8E3")) + theme_bw()
  
  #print(plot)
  
  plot_list[[file_name]] <- plot
}
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`
arranged_plots <- arrangeGrob(grobs = plot_list, ncol = 3)
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_line()`).
gridExtra::grid.arrange(arranged_plots)

ggsave("Plots/Polymerized041024_Measured041224_plots.png", arranged_plots, width = 16, height = 9, units = "in", device = "png")
files <- list.files(path = "./Polymerized041024_Measured041224", pattern = "\\.csv$", full.names = TRUE)

plot_list <- list()

for (file in files) {
  file_name <- tools::file_path_sans_ext(basename(file))
   
  file_data <- read_csv(paste(file),show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>% filter(Condition == "Light" | Condition == "Dark")
  
  # Determine the x-limits and color for the rectangle based on the file name
  if (grepl("1000", file_name)) {
    rect_x <- c(0, 1000)
  } else {
    rect_x <- c(0, 200)
  }
  
  plot <- ggplot(data = file_data, aes(y = `Oxygen (µmol/L)`, x = `Depth (µm)`,
                                       color = `Condition`, shape = `Replicate`)) + 
      geom_rect(aes(xmin = rect_x[1], xmax = rect_x[2], ymin = -Inf, ymax = Inf), fill = "tan", color = "tan", alpha = 0.5) +
      geom_point() + geom_line() +
      coord_flip() + scale_x_reverse() + xlim(1001, -1450) + ylim(140, 405) + 
      geom_vline(xintercept = 0, linetype = 3) + 
      theme_bw() + ggtitle(file_name) +
      annotate("text", x = -1400, y = 395, label = paste("Start:", file_data$Time[1]), vjust = 1, hjust = 1, size = 3) +
      annotate("text", x = -1300, y = 395, label = paste("End:", file_data$Time[nrow(file_data)]), vjust = 1, hjust = 1, size = 3) +
    theme(legend.position = "bottom")
  
  print(plot)
}
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_line()`).
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

Extra profiles taken not plotted above:

files <- c("./Polymerized041024_Measured041224/Gel041024_1000um_1.csv", "./Polymerized041024_Measured041224/Gel041024_200um_3.csv")

plot_list <- list()

for (file in files) {
  file_name <- tools::file_path_sans_ext(basename(file))
  
  file_data <- read_csv(paste(file),show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>% filter(Condition != "Light" & Condition != "Dark")

 plot <- ggplot(data = file_data, aes(y = `Oxygen (µmol/L)`, x = `Depth (µm)`,
                                       color = `Condition`, shape = `Replicate`)) + 
      geom_point() + geom_line() +
      coord_flip() + scale_x_reverse() + xlim(0, -1450) +  
      geom_vline(xintercept = 0, linetype = 3) + 
      theme_bw() + ggtitle(file_name) +
      annotate("text", x = -1400, y = 295, label = paste("Start:", file_data$Time[1]), vjust = 1, hjust = 1, size = 3) +
      annotate("text", x = -1300, y = 295, label = paste("End:", file_data$Time[nrow(file_data)]), vjust = 1, hjust = 1, size = 3) +
    theme(legend.position = "bottom")
  
  #print(plot)
  
  plot_list[[file_name]] <- plot
}
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`
arranged_plots <- arrangeGrob(grobs = plot_list, ncol = 3)
## Warning: Removed 30 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 30 rows containing missing values or values outside the scale range
## (`geom_line()`).
gridExtra::grid.arrange(arranged_plots)

ggsave("Plots/Polymerized041024_Measured041224_plots_extra.png", arranged_plots, width = 16, height = 9, units = "in", device = "png")

Using segmented linear regression, segmented R package

library(segmented)

# Function to perform segmented regression and extract slope after breakpoint
perform_segmented_regression <- function(oxygen_concentration, depth) {
  # Fit segmented regression model
  fit <- segmented(lm(oxygen_concentration ~ depth), seg.Z = ~depth, psi = -500)
  
  # Extract the breakpoint(s)
  breakpoint <- fit$psi[, 2]
  
  # Find the index of the datapoint nearest to the breakpoint
  breakpoint_index <- which.min(abs(depth - breakpoint))

  # Extract the oxygen concentration at the breakpoint
  Oxygen_breakpoint <- oxygen_concentration[breakpoint_index]
  
  # Extract the oxygen concentration at depth = 0
  Oxygen_Surface <- oxygen_concentration[which(depth == 0)]
  
  # Extract the slope after the breakpoint for each segment
  lm_fit <- lm(oxygen_concentration ~ depth,
                 data = data.frame(depth = depth[breakpoint_index:length(depth)],
                                   oxygen_concentration = oxygen_concentration[breakpoint_index:length(oxygen_concentration)]))
  slope <- coef(lm_fit)["depth"]
  
  # Calculate fitted values for segmented regression lines
  fitted_values <- fitted(fit, newdata = data.frame(depth = depth))
  
  # Create dataframe with depth and fitted oxygen concentration values
  fitted_data <- data.frame("Depth" = depth, "Oxygen" = fitted_values)

  return(list(
    slope = slope,
    breakpoint = breakpoint,
    Oxygen_breakpoint = Oxygen_breakpoint,
    Oxygen_Surface = Oxygen_Surface,
    fitted_data = fitted_data
  ))
}

results_df_1000um_segmented <- data.frame(
  File = character(),
  Scaffold_Thickness = character(),
  Replicate = character(),
  slope = numeric(),
  Flux = numeric(),
  Breakpoint = numeric(),
  Oxygen_Surface = numeric(),
  Oxygen_breakpoint = numeric(),
  stringsAsFactors = FALSE
)

# Get list of CSV file names with "1000" in the title
file_list <- list.files(path = "./Polymerized041024_Measured041224", pattern = "1000.*\\.csv$", full.names = TRUE)

# Loop over files
for (file_path in file_list) {
  # Read data from file
  data <- read_csv(file_path, show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>%
    arrange(`Depth (µm)`)
  
  # Loop over replicates
  for (replicate_id in unique(data$Replicate)) {
    # Subset data for the current replicate
    replicate_data <- data %>%
      filter(Replicate == replicate_id & Condition == "Light") %>%
      filter(`Depth (µm)` <= 0)
    
    # Extract oxygen concentration and depth
    oxygen_concentration <- replicate_data %>% pull(`Oxygen (µmol/L)`)
    depth <- replicate_data %>% pull(`Depth (µm)`)
    
    # Perform segmented regression
    regression_results <- perform_segmented_regression(oxygen_concentration, depth)
    
    # Plot data and segmented regression lines
    p <- ggplot(replicate_data, aes(x = `Depth (µm)`, y = `Oxygen (µmol/L)`)) +
      geom_point() +
      geom_line(data = regression_results$fitted_data, aes(x = `Depth`, y = `Oxygen`), color = "red") +
      labs(title = paste("Replicate", replicate_id), x = "Depth (µm)", y = "Oxygen (µmol/L)")
    
    print(p)
    
    # Calculate flux using Fick’s first law of diffusion
    DO2 <- 2.2417e-5  # Diffusion coefficient of oxygen
    flux <- DO2 * regression_results$slope
        
    # Store results in dataframe
    results_df_1000um_segmented <- bind_rows(results_df_1000um_segmented, data.frame(
      File = basename(file_path),
      Scaffold_Thickness = "1000 um",
      Replicate = replicate_id,
      slope = regression_results$slope,
      Flux = flux,  
      Breakpoint = regression_results$breakpoint,  
      Oxygen_Surface = regression_results$Oxygen_Surface, 
      Oxygen_breakpoint = regression_results$Oxygen_breakpoint,
      stringsAsFactors = FALSE
    ))
  }
}
## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

# Print the results dataframe
print(results_df_1000um_segmented)
##                             File Scaffold_Thickness Replicate     slope
## depth...1 Gel041024_1000um_1.csv            1000 um         1 0.1886047
## depth...2 Gel041024_1000um_1.csv            1000 um         2 0.1865547
## depth...3 Gel041024_1000um_1.csv            1000 um         3 0.1909753
## depth...4 Gel041024_1000um_2.csv            1000 um         1 0.1564983
## depth...5 Gel041024_1000um_2.csv            1000 um         2 0.1645147
## depth...6 Gel041024_1000um_2.csv            1000 um         3 0.1813063
## depth...7 Gel041024_1000um_3.csv            1000 um         1 0.2525014
## depth...8 Gel041024_1000um_3.csv            1000 um         2 0.1921323
## depth...9 Gel041024_1000um_3.csv            1000 um         3 0.1993643
##                   Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1 4.227951e-06  -417.4311        288.860           216.726
## depth...2 4.181996e-06  -418.5536        289.131           216.760
## depth...3 4.281094e-06  -389.4811        288.271           215.303
## depth...4 3.508223e-06  -380.1215        273.166           212.490
## depth...5 3.687925e-06  -387.7747        275.934           212.737
## depth...6 4.064344e-06  -412.8095        280.751           211.682
## depth...7 5.660325e-06  -321.0366        288.669           216.095
## depth...8 4.307031e-06  -385.5178        287.524           212.723
## depth...9 4.469150e-06  -385.2196        291.139           213.216
results_df_200um_segmented <- data.frame(
  File = character(),
  Scaffold_Thickness = character(),
  Replicate = character(),
  slope = numeric(),
  Flux = numeric(),
  Breakpoint = numeric(),
  Oxygen_Surface = numeric(),
  Oxygen_breakpoint = numeric(),
  stringsAsFactors = FALSE
)

# Get list of CSV file names with "200" in the title
file_list <- list.files(path = "./Polymerized041024_Measured041224", pattern = "200.*\\.csv$", full.names = TRUE)

# Loop over files
for (file_path in file_list) {
  # Read data from file
  data <- read_csv(file_path, show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>%
    arrange(`Depth (µm)`)
  
  # Loop over replicates
  for (replicate_id in unique(data$Replicate)) {
    # Subset data for the current replicate
    replicate_data <- data %>%
      filter(Replicate == replicate_id & Condition == "Light") %>%
      filter(`Depth (µm)` <= 0)
    
    # Extract oxygen concentration and depth
    oxygen_concentration <- replicate_data %>% pull(`Oxygen (µmol/L)`)
    depth <- replicate_data %>% pull(`Depth (µm)`)
    
    # Perform segmented regression
    regression_results <- perform_segmented_regression(oxygen_concentration, depth)
    
    # Plot data and segmented regression lines
    p <- ggplot(replicate_data, aes(x = `Depth (µm)`, y = `Oxygen (µmol/L)`)) +
      geom_point() +
      geom_line(data = regression_results$fitted_data, aes(x = `Depth`, y = `Oxygen`), color = "red") +
      labs(title = paste("Replicate", replicate_id), x = "Depth (µm)", y = "Oxygen (µmol/L)")
    
    print(p)
    
    # Calculate flux using Fick’s first law of diffusion
    DO2 <- 2.2417e-5  # Diffusion coefficient of oxygen
    flux <- DO2 * regression_results$slope
        
    # Store results in dataframe
    results_df_200um_segmented <- bind_rows(results_df_200um_segmented, data.frame(
      File = basename(file_path),
      Scaffold_Thickness = "200 um",
      Replicate = replicate_id,
      slope = regression_results$slope,
      Flux = flux,  
      Breakpoint = regression_results$breakpoint,  
      Oxygen_Surface = regression_results$Oxygen_Surface, 
      Oxygen_breakpoint = regression_results$Oxygen_breakpoint,
      stringsAsFactors = FALSE
    ))
  }
}
## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

# Print the results dataframe
print(results_df_200um_segmented)
##                            File Scaffold_Thickness Replicate     slope
## depth...1 Gel041024_200um_1.csv             200 um         1 0.2021880
## depth...2 Gel041024_200um_1.csv             200 um         2 0.2184340
## depth...3 Gel041024_200um_1.csv             200 um         3 0.2466440
## depth...4 Gel041024_200um_2.csv             200 um         1 0.1583019
## depth...5 Gel041024_200um_2.csv             200 um         2 0.1779883
## depth...6 Gel041024_200um_2.csv             200 um         3 0.1804715
## depth...7 Gel041024_200um_3.csv             200 um         1 0.1273800
## depth...8 Gel041024_200um_3.csv             200 um         2 0.1296086
## depth...9 Gel041024_200um_3.csv             200 um         3 0.1346250
##                   Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1 4.532448e-06  -423.0096        284.485           207.667
## depth...2 4.896635e-06  -386.3812        289.267           205.874
## depth...3 5.529019e-06  -385.2487        294.029           202.154
## depth...4 3.548654e-06  -373.7823        258.055           204.006
## depth...5 3.989964e-06  -420.7698        272.079           203.448
## depth...6 4.045630e-06  -438.4452        279.738           201.503
## depth...7 2.855477e-06  -276.4187        234.767           197.122
## depth...8 2.905435e-06  -277.3252        235.705           198.117
## depth...9 3.017889e-06  -278.9906        236.974           197.286

Stats

Full_results_segmented_041224 <- rbind(results_df_1000um_segmented, results_df_200um_segmented)

Full_results_segmented_041224
##                              File Scaffold_Thickness Replicate     slope
## depth...1  Gel041024_1000um_1.csv            1000 um         1 0.1886047
## depth...2  Gel041024_1000um_1.csv            1000 um         2 0.1865547
## depth...3  Gel041024_1000um_1.csv            1000 um         3 0.1909753
## depth...4  Gel041024_1000um_2.csv            1000 um         1 0.1564983
## depth...5  Gel041024_1000um_2.csv            1000 um         2 0.1645147
## depth...6  Gel041024_1000um_2.csv            1000 um         3 0.1813063
## depth...7  Gel041024_1000um_3.csv            1000 um         1 0.2525014
## depth...8  Gel041024_1000um_3.csv            1000 um         2 0.1921323
## depth...9  Gel041024_1000um_3.csv            1000 um         3 0.1993643
## depth...11  Gel041024_200um_1.csv             200 um         1 0.2021880
## depth...21  Gel041024_200um_1.csv             200 um         2 0.2184340
## depth...31  Gel041024_200um_1.csv             200 um         3 0.2466440
## depth...41  Gel041024_200um_2.csv             200 um         1 0.1583019
## depth...51  Gel041024_200um_2.csv             200 um         2 0.1779883
## depth...61  Gel041024_200um_2.csv             200 um         3 0.1804715
## depth...71  Gel041024_200um_3.csv             200 um         1 0.1273800
## depth...81  Gel041024_200um_3.csv             200 um         2 0.1296086
## depth...91  Gel041024_200um_3.csv             200 um         3 0.1346250
##                    Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1  4.227951e-06  -417.4311        288.860           216.726
## depth...2  4.181996e-06  -418.5536        289.131           216.760
## depth...3  4.281094e-06  -389.4811        288.271           215.303
## depth...4  3.508223e-06  -380.1215        273.166           212.490
## depth...5  3.687925e-06  -387.7747        275.934           212.737
## depth...6  4.064344e-06  -412.8095        280.751           211.682
## depth...7  5.660325e-06  -321.0366        288.669           216.095
## depth...8  4.307031e-06  -385.5178        287.524           212.723
## depth...9  4.469150e-06  -385.2196        291.139           213.216
## depth...11 4.532448e-06  -423.0096        284.485           207.667
## depth...21 4.896635e-06  -386.3812        289.267           205.874
## depth...31 5.529019e-06  -385.2487        294.029           202.154
## depth...41 3.548654e-06  -373.7823        258.055           204.006
## depth...51 3.989964e-06  -420.7698        272.079           203.448
## depth...61 4.045630e-06  -438.4452        279.738           201.503
## depth...71 2.855477e-06  -276.4187        234.767           197.122
## depth...81 2.905435e-06  -277.3252        235.705           198.117
## depth...91 3.017889e-06  -278.9906        236.974           197.286
mean_flux <- Full_results_segmented_041224 %>%
  group_by(Scaffold_Thickness) %>%
  summarize(mean_flux = mean(Flux),
            min = min(Flux))

# Running ANOVA
anova_result <- aov(Flux ~ Scaffold_Thickness, data = Full_results_segmented_041224)
summary(anova_result)
##                    Df    Sum Sq   Mean Sq F value Pr(>F)
## Scaffold_Thickness  1 5.230e-13 5.225e-13   0.836  0.374
## Residuals          16 9.997e-12 6.248e-13
# What about for oxygen surface concentration?

mean_Oxygen_Surface <- Full_results_segmented_041224 %>%
  group_by(Scaffold_Thickness) %>%
  summarize(mean_Oxygen_Surface = mean(Oxygen_Surface),
            min = min(Oxygen_Surface))

# Running ANOVA
anova_result <- aov(Oxygen_Surface ~ Scaffold_Thickness, data = Full_results_segmented_041224)
summary(anova_result)
##                    Df Sum Sq Mean Sq F value Pr(>F)  
## Scaffold_Thickness  1   1767    1767   5.627 0.0306 *
## Residuals          16   5025     314                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# What about DBL Thickness?

mean_DBL_end <- Full_results_segmented_041224 %>%
  group_by(Scaffold_Thickness) %>%
  summarize(mean_DBL_end = mean(Breakpoint),
            max = max(Breakpoint))

# Running ANOVA
anova_result <- aov(Breakpoint ~ Scaffold_Thickness, data = Full_results_segmented_041224)
summary(anova_result)
##                    Df Sum Sq Mean Sq F value Pr(>F)
## Scaffold_Thickness  1   3136    3136   1.176  0.294
## Residuals          16  42651    2666

Combined plots

library(cowplot)

# Plotting the flux values for each thickness
flux_plot <- ggplot(Full_results_segmented_041224, aes(x = Scaffold_Thickness, y = Flux)) +
  geom_boxplot() + geom_point(aes(color = File), show.legend = FALSE, position = position_jitter(width = 0.05)) + 
  geom_text(data = mean_flux, aes(label = paste("Mean Flux =\n", sprintf("%.2e", mean_flux)),
                                  y = min-.0000005), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Thickness (µm)", y = "Flux") + theme_bw()

# Plotting the surface O2 values for each thickness
oxygen_surface_plot <- ggplot(Full_results_segmented_041224, aes(x = Scaffold_Thickness, y = Oxygen_Surface)) +
  geom_boxplot() + geom_point(aes(color = File), show.legend = FALSE, position = position_jitter(width = 0.05)) + 
  geom_text(data = mean_Oxygen_Surface, aes(label = paste("Mean Surface O2 =\n", round(mean_Oxygen_Surface, 1)),
                                            y = min-7), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Thickness (µm)", y = "Oxygen Surface Concentration (µmol/L)") + theme_bw()

# Plotting the DBL Thickness for each scaffold thickness
dbl_thickness_plot <- ggplot(Full_results_segmented_041224, aes(x = Scaffold_Thickness, y = -Breakpoint)) +
  geom_boxplot() + geom_point(aes(color = File), show.legend = FALSE, position = position_jitter(width = 0.05)) + 
  geom_text(data = mean_DBL_end, aes(label = paste("Mean DBL Thickness =\n", round(-mean_DBL_end, 1)),
                                     y = -max-22), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Thickness (µm)", y = "DBL Thickness (µm)") + theme_bw()

# Combine plots with a common legend at the bottom
combined_plot <- plot_grid(flux_plot, oxygen_surface_plot, dbl_thickness_plot, nrow = 1, align = "v", rel_heights = c(1, 1, 1))

# Add overarching title
combined_plot_with_title <- ggdraw() +
  draw_label("Comparison of Flux, Oxygen Surface Concentration, and DBL Thickness\nbetween Hydrogel Thicknesses; Same areal density (4/12/24)",
             x = 0.5, y = 0.98, # Adjust position as needed
             hjust = 0.5, vjust = 1,
             size = 12, fontface = "bold")

# Combine title and plots
final_plot <- plot_grid(combined_plot_with_title, combined_plot, ncol = 1, rel_heights = c(0.1, 1))

ggsave("Plots/Polymerized041024_Measured041224_Data_plot.png", final_plot, device = "png", height = 5, width = 12)

#ggsave("Plots/Polymerized041024_Measured041224_Flux_plot.png", flux_plot, device = "png")

Plotting profiles with DBL

files <- list.files(path = "./Polymerized041024_Measured041224", pattern = "\\.csv$", full.names = TRUE)

plot_list <- list()

for (file in files) {
  file_name <- tools::file_path_sans_ext(basename(file))
  
  file_data <- read_csv(paste(file),show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>% filter(Condition == "Light" | Condition == "Dark")
  
  # Extract the DBL depth for the current file
  dbl_depth <- Full_results_segmented_041224$Breakpoint[gsub("\\.csv$", "", Full_results_segmented_041224$File) == file_name]

  plot <- ggplot(data = file_data, aes(y = `Oxygen (µmol/L)`, x = `Depth (µm)`,
                                       color = `Condition`, shape = `Replicate`)) + 
      geom_point() + geom_line() +
      coord_flip() + scale_x_reverse() + xlim(0, -1000) + ylim(140, 405) +
      theme_bw() + ggtitle(file_name) +
      annotate("text", x = -900, y = 395, label = paste("Start:", file_data$Time[1]), vjust = 1, hjust = 1, size = 3) +
      annotate("text", x = -850, y = 395, label = paste("End:", file_data$Time[nrow(file_data)]), vjust = 1, hjust = 1, size = 3) +
      geom_vline(xintercept = dbl_depth, linetype = "dashed") +  # Add DBL depth line
      theme(legend.position = "bottom") + scale_color_manual(values = c("#4169E1", "#7EC8E3")) + theme_bw()
  
  #print(plot)
  
  plot_list[[file_name]] <- plot
}
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`
arranged_plots <- arrangeGrob(grobs = plot_list, ncol = 3)
## Warning: Removed 159 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 159 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 156 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 156 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 156 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 156 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 60 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 60 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 66 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 66 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 66 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 66 rows containing missing values or values outside the scale range
## (`geom_line()`).
gridExtra::grid.arrange(arranged_plots)

ggsave("Plots/Polymerized041024_Measured041224_plots_DBL.png", arranged_plots, width = 16, height = 9, units = "in", device = "png")

Third Dataset, 4/13/24

Analyze dataset from 4/13/24, for the gels (numbered 1-3) made on 4/08/24. These gels were stressed out using high light (PAR 630 umol/m^2/s) for two hours from 9AM-11AM in the morning in the incubator they were kept in and then held at ambient light (PAR = 90) until profiles were taken. There is an n = 3 for each thickness, with 3 gels of 200 um thickness (“thin”) and 3 gels of 1000 um thickness (“thick”). Three profiles were taken per gel, at a light intensity of PAR 90 umol/m^2/s and a flow rate of 0.5 cm/s. Three additional profiles were taken per gel in high light (PAR 630 umol/m^2/s) after 5-10 minutes of adaptation to equilibrium based on the oxygen surface concentration no longer increasing and reaching steady state. The profiles were taken in the middle of the hydrogel, in the same position for all 6 profiles per gel. Profiles were taken partially into the depth of the gel, which is shown in “positive” depths.

Plotting profiles

files <- list.files(path = "./Polymerized040824_Measured041324", pattern = "\\.csv$", full.names = TRUE)

plot_list <- list()

for (file in files) {
  file_name <- tools::file_path_sans_ext(basename(file))
  
  file_data <- read_csv(paste(file),show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) #%>% filter(Condition == "Light" | Condition == "Dark")
  
  plot <- ggplot(data = file_data, aes(y = `Oxygen (µmol/L)`, x = `Depth (µm)`,
                                       color = `Condition`, shape = `Replicate`)) + 
      geom_point() + geom_line() +
      coord_flip() + scale_x_reverse() + xlim(1000, -1450) + ylim(200, 900) + 
      geom_vline(xintercept = 0, linetype = 3) + 
      theme_bw() + ggtitle(file_name) +
      annotate("text", x = -1400, y = 395, label = paste("Start:", file_data$Time[1]), vjust = 1, hjust = 1, size = 3) +
      annotate("text", x = -1300, y = 395, label = paste("End:", file_data$Time[nrow(file_data)]), vjust = 1, hjust = 1, size = 3) +
    theme(legend.position = "bottom") + scale_color_manual(values = c("#FFA500","#7EC8E3")) + theme_bw()
  
  #print(plot)
  
  plot_list[[file_name]] <- plot
}
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`
arranged_plots <- arrangeGrob(grobs = plot_list, ncol = 3)

gridExtra::grid.arrange(arranged_plots)

ggsave("Plots/Polymerized040824_Measured041324_plots.png", arranged_plots, width = 16, height = 9, units = "in", device = "png")
files <- list.files(path = "./Polymerized040824_Measured041324", pattern = "\\.csv$", full.names = TRUE)

plot_list <- list()

for (file in files) {
  file_name <- tools::file_path_sans_ext(basename(file))
   
  file_data <- read_csv(paste(file),show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) #%>% filter(Condition == "Light" | Condition == "Dark")
  
  # Determine the x-limits and color for the rectangle based on the file name
  if (grepl("1000", file_name)) {
    rect_x <- c(0, 1000)
  } else {
    rect_x <- c(0, 200)
  }
  
  plot <- ggplot(data = file_data, aes(y = `Oxygen (µmol/L)`, x = `Depth (µm)`,
                                       color = `Condition`, shape = `Replicate`)) + 
      geom_rect(aes(xmin = rect_x[1], xmax = rect_x[2], ymin = -Inf, ymax = Inf), fill = "tan", color = "tan", alpha = 0.5) +
      geom_point() + geom_line() +
      coord_flip() + scale_x_reverse() + xlim(1001, -1450) + ylim(200, 900) + 
      geom_vline(xintercept = 0, linetype = 3) + 
      theme_bw() + ggtitle(file_name) +
      annotate("text", x = -1400, y = 395, label = paste("Start:", file_data$Time[1]), vjust = 1, hjust = 1, size = 3) +
      annotate("text", x = -1300, y = 395, label = paste("End:", file_data$Time[nrow(file_data)]), vjust = 1, hjust = 1, size = 3) +
    theme(legend.position = "bottom")
  
  print(plot)
}
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

Using segmented linear regression, segmented R package

library(segmented)

# Function to perform segmented regression and extract slope after breakpoint
perform_segmented_regression <- function(oxygen_concentration, depth) {
  # Fit segmented regression model
  fit <- segmented(lm(oxygen_concentration ~ depth), seg.Z = ~depth, psi = -250)
  
  # Extract the breakpoint(s)
  breakpoint <- fit$psi[, 2]
  
  # Find the index of the datapoint nearest to the breakpoint
  breakpoint_index <- which.min(abs(depth - breakpoint))

  # Extract the oxygen concentration at the breakpoint
  Oxygen_breakpoint <- oxygen_concentration[breakpoint_index]
  
  # Extract the oxygen concentration at depth = 0
  Oxygen_Surface <- oxygen_concentration[which(depth == 0)]
  
  # Extract the slope after the breakpoint for each segment
  lm_fit <- lm(oxygen_concentration ~ depth,
                 data = data.frame(depth = depth[breakpoint_index:length(depth)],
                                   oxygen_concentration = oxygen_concentration[breakpoint_index:length(oxygen_concentration)]))
  slope <- coef(lm_fit)["depth"]
  
  # Calculate fitted values for segmented regression lines
  fitted_values <- fitted(fit, newdata = data.frame(depth = depth))
  
  # Create dataframe with depth and fitted oxygen concentration values
  fitted_data <- data.frame("Depth" = depth, "Oxygen" = fitted_values)

  return(list(
    slope = slope,
    breakpoint = breakpoint,
    Oxygen_breakpoint = Oxygen_breakpoint,
    Oxygen_Surface = Oxygen_Surface,
    fitted_data = fitted_data
  ))
}

results_df_1000um_segmented <- data.frame(
  File = character(),
  Scaffold_Thickness = character(),
  Replicate = character(),
  slope = numeric(),
  Flux = numeric(),
  Breakpoint = numeric(),
  Oxygen_Surface = numeric(),
  Oxygen_breakpoint = numeric(),
  stringsAsFactors = FALSE
)

# Get list of CSV file names with "1000" in the title
file_list <- list.files(path = "./Polymerized040824_Measured041324", pattern = "1000.*\\.csv$", full.names = TRUE)

# Loop over files
for (file_path in file_list) {
  # Read data from file
  data <- read_csv(file_path, show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>%
    arrange(`Depth (µm)`)
  
  # Loop over replicates
  for (replicate_id in unique(data$Replicate)) {
    # Subset data for the current replicate
    replicate_data <- data %>%
      filter(Replicate == replicate_id & Condition == "Light")  %>%
      filter(`Depth (µm)` <= 0)
    
    # Extract oxygen concentration and depth
    oxygen_concentration <- replicate_data %>% pull(`Oxygen (µmol/L)`)
    depth <- replicate_data %>% pull(`Depth (µm)`)
    
    # Perform segmented regression
    regression_results <- perform_segmented_regression(oxygen_concentration, depth)
    
    # Plot data and segmented regression lines
    p <- ggplot(replicate_data, aes(x = `Depth (µm)`, y = `Oxygen (µmol/L)`)) +
      geom_point() +
      geom_line(data = regression_results$fitted_data, aes(x = `Depth`, y = `Oxygen`), color = "red") +
      labs(title = paste("Replicate", replicate_id), x = "Depth (µm)", y = "Oxygen (µmol/L)")
    
    print(p)
    
    # Calculate flux using Fick’s first law of diffusion
    DO2 <- 2.2417e-5  # Diffusion coefficient of oxygen
    flux <- DO2 * regression_results$slope
        
    # Store results in dataframe
    results_df_1000um_segmented <- bind_rows(results_df_1000um_segmented, data.frame(
      File = basename(file_path),
      Scaffold_Thickness = "1000 um",
      Replicate = replicate_id,
      slope = regression_results$slope,
      Flux = flux,  
      Breakpoint = regression_results$breakpoint,  
      Oxygen_Surface = regression_results$Oxygen_Surface, 
      Oxygen_breakpoint = regression_results$Oxygen_breakpoint,
      stringsAsFactors = FALSE
    ))
  }
}
## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

# Print the results dataframe
print(results_df_1000um_segmented)
##                             File Scaffold_Thickness Replicate     slope
## depth...1 Gel040824_1000um_1.csv            1000 um         1 0.5821080
## depth...2 Gel040824_1000um_1.csv            1000 um         2 0.5695080
## depth...3 Gel040824_1000um_1.csv            1000 um         3 0.5511640
## depth...4 Gel040824_1000um_2.csv            1000 um         1 0.5025440
## depth...5 Gel040824_1000um_2.csv            1000 um         2 0.4722500
## depth...6 Gel040824_1000um_2.csv            1000 um         3 0.4822000
## depth...7 Gel040824_1000um_3.csv            1000 um         1 0.3841909
## depth...8 Gel040824_1000um_3.csv            1000 um         2 0.4481669
## depth...9 Gel040824_1000um_3.csv            1000 um         3 0.4936017
##                   Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1 1.304912e-05  -222.7569        350.297           235.901
## depth...2 1.276666e-05  -214.8453        342.694           230.712
## depth...3 1.235544e-05  -239.9065        360.905           227.053
## depth...4 1.126553e-05  -223.8684        327.998           231.279
## depth...5 1.058643e-05  -217.2484        317.507           225.020
## depth...6 1.080948e-05  -214.3318        318.232           223.906
## depth...7 8.612406e-06  -234.9614        312.305           218.707
## depth...8 1.004656e-05  -231.9015        328.562           218.860
## depth...9 1.106507e-05  -229.3515        341.538           220.488
results_df_200um_segmented <- data.frame(
  File = character(),
  Scaffold_Thickness = character(),
  Replicate = character(),
  slope = numeric(),
  Flux = numeric(),
  Breakpoint = numeric(),
  Oxygen_Surface = numeric(),
  Oxygen_breakpoint = numeric(),
  stringsAsFactors = FALSE
)

# Get list of CSV file names with "200" in the title
file_list <- list.files(path = "./Polymerized040824_Measured041324", pattern = "200.*\\.csv$", full.names = TRUE)

# Loop over files
for (file_path in file_list) {
  # Read data from file
  data <- read_csv(file_path, show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>%
    arrange(`Depth (µm)`)
  
  # Loop over replicates
  for (replicate_id in unique(data$Replicate)) {
    # Subset data for the current replicate
    replicate_data <- data %>%
      filter(Replicate == replicate_id & Condition == "Light") %>%
      filter(`Depth (µm)` <= 0)
    
    # Extract oxygen concentration and depth
    oxygen_concentration <- replicate_data %>% pull(`Oxygen (µmol/L)`)
    depth <- replicate_data %>% pull(`Depth (µm)`)
    
    # Perform segmented regression
    regression_results <- perform_segmented_regression(oxygen_concentration, depth)
    
    # Plot data and segmented regression lines
    p <- ggplot(replicate_data, aes(x = `Depth (µm)`, y = `Oxygen (µmol/L)`)) +
      geom_point() +
      geom_line(data = regression_results$fitted_data, aes(x = `Depth`, y = `Oxygen`), color = "red") +
      labs(title = paste("Replicate", replicate_id), x = "Depth (µm)", y = "Oxygen (µmol/L)")
    
    print(p)
    
    # Calculate flux using Fick’s first law of diffusion
    DO2 <- 2.2417e-5  # Diffusion coefficient of oxygen
    flux <- DO2 * regression_results$slope
        
    # Store results in dataframe
    results_df_200um_segmented <- bind_rows(results_df_200um_segmented, data.frame(
      File = basename(file_path),
      Scaffold_Thickness = "200 um",
      Replicate = replicate_id,
      slope = regression_results$slope,
      Flux = flux,  
      Breakpoint = regression_results$breakpoint,  
      Oxygen_Surface = regression_results$Oxygen_Surface, 
      Oxygen_breakpoint = regression_results$Oxygen_breakpoint,
      stringsAsFactors = FALSE
    ))
  }
}
## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

# Print the results dataframe
print(results_df_200um_segmented)
##                            File Scaffold_Thickness Replicate     slope
## depth...1 Gel040824_200um_1.csv             200 um         1 0.2072080
## depth...2 Gel040824_200um_1.csv             200 um         2 0.2168434
## depth...3 Gel040824_200um_1.csv             200 um         3 0.2427029
## depth...4 Gel040824_200um_2.csv             200 um         1 0.2040657
## depth...5 Gel040824_200um_2.csv             200 um         2 0.2093743
## depth...6 Gel040824_200um_2.csv             200 um         3 0.2081051
## depth...7 Gel040824_200um_3.csv             200 um         1 0.2263164
## depth...8 Gel040824_200um_3.csv             200 um         2 0.2147954
## depth...9 Gel040824_200um_3.csv             200 um         3 0.2215040
##                   Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1 4.644982e-06  -232.9567        265.988           215.260
## depth...2 4.860979e-06  -229.2693        267.437           214.600
## depth...3 5.440670e-06  -267.6632        276.688           217.204
## depth...4 4.574541e-06  -271.7906        265.946           215.835
## depth...5 4.693543e-06  -268.1471        266.835           215.400
## depth...6 4.665093e-06  -262.7707        264.796           213.796
## depth...7 5.073335e-06  -319.0819        282.498           216.533
## depth...8 4.815069e-06  -236.1913        265.580           213.431
## depth...9 4.965455e-06  -224.9881        260.208           216.236
results_df_1000um_segmented_high_light <- data.frame(
  File = character(),
  Scaffold_Thickness = character(),
  Replicate = character(),
  slope = numeric(),
  Flux = numeric(),
  Breakpoint = numeric(),
  Oxygen_Surface = numeric(),
  Oxygen_breakpoint = numeric(),
  stringsAsFactors = FALSE
)

# Get list of CSV file names with "1000" in the title
file_list <- list.files(path = "./Polymerized040824_Measured041324", pattern = "1000.*\\.csv$", full.names = TRUE)

# Loop over files
for (file_path in file_list) {
  # Read data from file
  data <- read_csv(file_path, show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>%
    arrange(`Depth (µm)`)
  
  # Loop over replicates
  for (replicate_id in unique(data$Replicate)) {
    # Subset data for the current replicate
    replicate_data <- data %>%
      filter(Replicate == replicate_id & Condition == "High_Light")  %>%
      filter(`Depth (µm)` <= 0)
    
    # Extract oxygen concentration and depth
    oxygen_concentration <- replicate_data %>% pull(`Oxygen (µmol/L)`)
    depth <- replicate_data %>% pull(`Depth (µm)`)
    
    # Perform segmented regression
    regression_results <- perform_segmented_regression(oxygen_concentration, depth)
    
    # Plot data and segmented regression lines
    p <- ggplot(replicate_data, aes(x = `Depth (µm)`, y = `Oxygen (µmol/L)`)) +
      geom_point() +
      geom_line(data = regression_results$fitted_data, aes(x = `Depth`, y = `Oxygen`), color = "red") +
      labs(title = paste("Replicate", replicate_id), x = "Depth (µm)", y = "Oxygen (µmol/L)")
    
    print(p)
    
    # Calculate flux using Fick’s first law of diffusion
    DO2 <- 2.2417e-5  # Diffusion coefficient of oxygen
    flux <- DO2 * regression_results$slope
        
    # Store results in dataframe
    results_df_1000um_segmented_high_light <- bind_rows(results_df_1000um_segmented_high_light, data.frame(
      File = basename(file_path),
      Scaffold_Thickness = "1000 um",
      Replicate = replicate_id,
      slope = regression_results$slope,
      Flux = flux,  
      Breakpoint = regression_results$breakpoint,  
      Oxygen_Surface = regression_results$Oxygen_Surface, 
      Oxygen_breakpoint = regression_results$Oxygen_breakpoint,
      stringsAsFactors = FALSE
    ))
  }
}
## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

# Print the results dataframe
print(results_df_1000um_segmented_high_light)
##                             File Scaffold_Thickness Replicate     slope
## depth...1 Gel040824_1000um_1.csv            1000 um         1 1.1332900
## depth...2 Gel040824_1000um_1.csv            1000 um         2 0.9957303
## depth...3 Gel040824_1000um_1.csv            1000 um         3 0.9178150
## depth...4 Gel040824_1000um_2.csv            1000 um         1 0.8172260
## depth...5 Gel040824_1000um_2.csv            1000 um         2 0.8410579
## depth...6 Gel040824_1000um_2.csv            1000 um         3 0.8079250
## depth...7 Gel040824_1000um_3.csv            1000 um         1 0.7755071
## depth...8 Gel040824_1000um_3.csv            1000 um         2 0.7735736
## depth...9 Gel040824_1000um_3.csv            1000 um         3 0.7711079
##                   Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1 2.540496e-05  -210.4788        461.724           241.926
## depth...2 2.232129e-05  -265.5822        493.305           248.847
## depth...3 2.057466e-05  -313.5366        524.744           261.686
## depth...4 1.831975e-05  -338.2794        517.052           234.581
## depth...5 1.885399e-05  -315.0148        494.952           246.561
## depth...6 1.811125e-05  -323.7080        489.418           246.379
## depth...7 1.738454e-05  -280.7664        463.173           236.405
## depth...8 1.734120e-05  -317.5293        474.739           243.014
## depth...9 1.728592e-05  -322.6140        473.471           253.653
results_df_200um_segmented_high_light <- data.frame(
  File = character(),
  Scaffold_Thickness = character(),
  Replicate = character(),
  slope = numeric(),
  Flux = numeric(),
  Breakpoint = numeric(),
  Oxygen_Surface = numeric(),
  Oxygen_breakpoint = numeric(),
  stringsAsFactors = FALSE
)

# Get list of CSV file names with "200" in the title
file_list <- list.files(path = "./Polymerized040824_Measured041324", pattern = "200.*\\.csv$", full.names = TRUE)

# Loop over files
for (file_path in file_list) {
  # Read data from file
  data <- read_csv(file_path, show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>%
    arrange(`Depth (µm)`)
  
  # Loop over replicates
  for (replicate_id in unique(data$Replicate)) {
    # Subset data for the current replicate
    replicate_data <- data %>%
      filter(Replicate == replicate_id & Condition == "High_Light") %>%
      filter(`Depth (µm)` <= 0)
    
    # Extract oxygen concentration and depth
    oxygen_concentration <- replicate_data %>% pull(`Oxygen (µmol/L)`)
    depth <- replicate_data %>% pull(`Depth (µm)`)
    
    # Perform segmented regression
    regression_results <- perform_segmented_regression(oxygen_concentration, depth)
    
    # Plot data and segmented regression lines
    p <- ggplot(replicate_data, aes(x = `Depth (µm)`, y = `Oxygen (µmol/L)`)) +
      geom_point() +
      geom_line(data = regression_results$fitted_data, aes(x = `Depth`, y = `Oxygen`), color = "red") +
      labs(title = paste("Replicate", replicate_id), x = "Depth (µm)", y = "Oxygen (µmol/L)")
    
    print(p)
    
    # Calculate flux using Fick’s first law of diffusion
    DO2 <- 2.2417e-5  # Diffusion coefficient of oxygen
    flux <- DO2 * regression_results$slope
        
    # Store results in dataframe
    results_df_200um_segmented_high_light <- bind_rows(results_df_200um_segmented_high_light, data.frame(
      File = basename(file_path),
      Scaffold_Thickness = "200 um",
      Replicate = replicate_id,
      slope = regression_results$slope,
      Flux = flux,  
      Breakpoint = regression_results$breakpoint,  
      Oxygen_Surface = regression_results$Oxygen_Surface, 
      Oxygen_breakpoint = regression_results$Oxygen_breakpoint,
      stringsAsFactors = FALSE
    ))
  }
}
## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

## New names:
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`

# Print the results dataframe
print(results_df_200um_segmented_high_light)
##                            File Scaffold_Thickness Replicate     slope
## depth...1 Gel040824_200um_1.csv             200 um         1 0.4842636
## depth...2 Gel040824_200um_1.csv             200 um         2 0.5132648
## depth...3 Gel040824_200um_1.csv             200 um         3 0.4619070
## depth...4 Gel040824_200um_2.csv             200 um         1 0.5319988
## depth...5 Gel040824_200um_2.csv             200 um         2 0.5372857
## depth...6 Gel040824_200um_2.csv             200 um         3 0.4678754
## depth...7 Gel040824_200um_3.csv             200 um         1 0.4546383
## depth...8 Gel040824_200um_3.csv             200 um         2 0.5200586
## depth...9 Gel040824_200um_3.csv             200 um         3 0.5592671
##                   Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1 1.085574e-05  -337.2228        396.543           229.495
## depth...2 1.150586e-05  -364.5962        404.125           232.285
## depth...3 1.035457e-05  -381.7309        411.104           233.866
## depth...4 1.192582e-05  -328.6009        411.731           231.869
## depth...5 1.204433e-05  -371.4499        420.229           234.575
## depth...6 1.048836e-05  -431.0765        428.783           225.745
## depth...7 1.019163e-05  -377.0699        397.521           222.018
## depth...8 1.165815e-05  -369.5406        404.353           226.069
## depth...9 1.253709e-05  -366.5890        417.810           230.045

Stats

Full_results_segmented_041324 <- rbind(results_df_1000um_segmented, results_df_200um_segmented)

Full_results_segmented_041324
##                              File Scaffold_Thickness Replicate     slope
## depth...1  Gel040824_1000um_1.csv            1000 um         1 0.5821080
## depth...2  Gel040824_1000um_1.csv            1000 um         2 0.5695080
## depth...3  Gel040824_1000um_1.csv            1000 um         3 0.5511640
## depth...4  Gel040824_1000um_2.csv            1000 um         1 0.5025440
## depth...5  Gel040824_1000um_2.csv            1000 um         2 0.4722500
## depth...6  Gel040824_1000um_2.csv            1000 um         3 0.4822000
## depth...7  Gel040824_1000um_3.csv            1000 um         1 0.3841909
## depth...8  Gel040824_1000um_3.csv            1000 um         2 0.4481669
## depth...9  Gel040824_1000um_3.csv            1000 um         3 0.4936017
## depth...11  Gel040824_200um_1.csv             200 um         1 0.2072080
## depth...21  Gel040824_200um_1.csv             200 um         2 0.2168434
## depth...31  Gel040824_200um_1.csv             200 um         3 0.2427029
## depth...41  Gel040824_200um_2.csv             200 um         1 0.2040657
## depth...51  Gel040824_200um_2.csv             200 um         2 0.2093743
## depth...61  Gel040824_200um_2.csv             200 um         3 0.2081051
## depth...71  Gel040824_200um_3.csv             200 um         1 0.2263164
## depth...81  Gel040824_200um_3.csv             200 um         2 0.2147954
## depth...91  Gel040824_200um_3.csv             200 um         3 0.2215040
##                    Flux Breakpoint Oxygen_Surface Oxygen_breakpoint
## depth...1  1.304912e-05  -222.7569        350.297           235.901
## depth...2  1.276666e-05  -214.8453        342.694           230.712
## depth...3  1.235544e-05  -239.9065        360.905           227.053
## depth...4  1.126553e-05  -223.8684        327.998           231.279
## depth...5  1.058643e-05  -217.2484        317.507           225.020
## depth...6  1.080948e-05  -214.3318        318.232           223.906
## depth...7  8.612406e-06  -234.9614        312.305           218.707
## depth...8  1.004656e-05  -231.9015        328.562           218.860
## depth...9  1.106507e-05  -229.3515        341.538           220.488
## depth...11 4.644982e-06  -232.9567        265.988           215.260
## depth...21 4.860979e-06  -229.2693        267.437           214.600
## depth...31 5.440670e-06  -267.6632        276.688           217.204
## depth...41 4.574541e-06  -271.7906        265.946           215.835
## depth...51 4.693543e-06  -268.1471        266.835           215.400
## depth...61 4.665093e-06  -262.7707        264.796           213.796
## depth...71 5.073335e-06  -319.0819        282.498           216.533
## depth...81 4.815069e-06  -236.1913        265.580           213.431
## depth...91 4.965455e-06  -224.9881        260.208           216.236
mean_flux <- Full_results_segmented_041324 %>%
  group_by(Scaffold_Thickness) %>%
  summarize(mean_flux = mean(Flux),
            min = min(Flux))

# Running ANOVA
anova_result <- aov(Flux ~ Scaffold_Thickness, data = Full_results_segmented_041324)
summary(anova_result)
##                    Df    Sum Sq   Mean Sq F value   Pr(>F)    
## Scaffold_Thickness  1 1.794e-10 1.794e-10   175.3 4.88e-10 ***
## Residuals          16 1.637e-11 1.020e-12                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# What about for oxygen surface concentration?

mean_Oxygen_Surface <- Full_results_segmented_041324 %>%
  group_by(Scaffold_Thickness) %>%
  summarize(mean_Oxygen_Surface = mean(Oxygen_Surface),
            min = min(Oxygen_Surface))

# Running ANOVA
anova_result <- aov(Oxygen_Surface ~ Scaffold_Thickness, data = Full_results_segmented_041324)
summary(anova_result)
##                    Df Sum Sq Mean Sq F value   Pr(>F)    
## Scaffold_Thickness  1  18952   18952   119.1 8.03e-09 ***
## Residuals          16   2546     159                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# What about DBL Thickness?

mean_DBL_end <- Full_results_segmented_041324 %>%
  group_by(Scaffold_Thickness) %>%
  summarize(mean_DBL_end = mean(Breakpoint),
            max = max(Breakpoint))

# Running ANOVA
anova_result <- aov(Breakpoint ~ Scaffold_Thickness, data = Full_results_segmented_041324)
summary(anova_result)
##                    Df Sum Sq Mean Sq F value  Pr(>F)   
## Scaffold_Thickness  1   4471    4471    9.15 0.00805 **
## Residuals          16   7818     489                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Combined plots

library(cowplot)

# Plotting the flux values for each thickness
flux_plot <- ggplot(Full_results_segmented_041324, aes(x = Scaffold_Thickness, y = Flux)) +
  geom_boxplot() + geom_point(aes(color = File), show.legend = FALSE, position = position_jitter(width = 0.05)) + 
  geom_text(data = mean_flux, aes(label = paste("Mean Flux =\n", sprintf("%.2e", mean_flux)),
                                  y = min-.000001), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Thickness (µm)", y = "Flux") + theme_bw()

# Plotting the surface O2 values for each thickness
oxygen_surface_plot <- ggplot(Full_results_segmented_041324, aes(x = Scaffold_Thickness, y = Oxygen_Surface)) +
  geom_boxplot() + geom_point(aes(color = File), show.legend = FALSE, position = position_jitter(width = 0.05)) + 
  geom_text(data = mean_Oxygen_Surface, aes(label = paste("Mean Surface O2 =\n", round(mean_Oxygen_Surface, 1)),
                                            y = min-15), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Thickness (µm)", y = "Oxygen Surface Concentration (µmol/L)") + theme_bw()

# Plotting the DBL Thickness for each scaffold thickness
dbl_thickness_plot <- ggplot(Full_results_segmented_041324, aes(x = Scaffold_Thickness, y = -Breakpoint)) +
  geom_boxplot() + geom_point(aes(color = File), show.legend = FALSE, position = position_jitter(width = 0.05)) + 
  geom_text(data = mean_DBL_end, aes(label = paste("Mean DBL Thickness =\n", round(-mean_DBL_end, 1)),
                                     y = -max-15), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Thickness (µm)", y = "DBL Thickness (µm)") + theme_bw()

# Combine plots with a common legend at the bottom
combined_plot <- plot_grid(flux_plot, oxygen_surface_plot, dbl_thickness_plot, nrow = 1, align = "v", rel_heights = c(1, 1, 1))

# Add overarching title
combined_plot_with_title <- ggdraw() +
  draw_label("Comparison of Flux, Oxygen Surface Concentration, and DBL Thickness\nbetween Hydrogel Thicknesses; Same volumetric density (4/13/24)",
             x = 0.5, y = 0.98, # Adjust position as needed
             hjust = 0.5, vjust = 1,
             size = 12, fontface = "bold")

# Combine title and plots
final_plot <- plot_grid(combined_plot_with_title, combined_plot, ncol = 1, rel_heights = c(0.1, 1))

ggsave("Plots/Polymerized040824_Measured041324_Data_plot.png", final_plot, device = "png", height = 5, width = 12)

#ggsave("Plots/Polymerized040824_Measured041324_Flux_plot.png", flux_plot, device = "png")

Stats with high light comparison

# Add a column indicating the condition for each dataframe
results_df_1000um_segmented$condition <- "Light"
results_df_200um_segmented$condition <- "Light"
results_df_1000um_segmented_high_light$condition <- "High_Light"
results_df_200um_segmented_high_light$condition <- "High_Light"

Full_results_segmented_041324_High_Light <- rbind(results_df_1000um_segmented, results_df_200um_segmented,results_df_1000um_segmented_high_light,results_df_200um_segmented_high_light)

Full_results_segmented_041324_High_Light
##                              File Scaffold_Thickness Replicate     slope
## depth...1  Gel040824_1000um_1.csv            1000 um         1 0.5821080
## depth...2  Gel040824_1000um_1.csv            1000 um         2 0.5695080
## depth...3  Gel040824_1000um_1.csv            1000 um         3 0.5511640
## depth...4  Gel040824_1000um_2.csv            1000 um         1 0.5025440
## depth...5  Gel040824_1000um_2.csv            1000 um         2 0.4722500
## depth...6  Gel040824_1000um_2.csv            1000 um         3 0.4822000
## depth...7  Gel040824_1000um_3.csv            1000 um         1 0.3841909
## depth...8  Gel040824_1000um_3.csv            1000 um         2 0.4481669
## depth...9  Gel040824_1000um_3.csv            1000 um         3 0.4936017
## depth...11  Gel040824_200um_1.csv             200 um         1 0.2072080
## depth...21  Gel040824_200um_1.csv             200 um         2 0.2168434
## depth...31  Gel040824_200um_1.csv             200 um         3 0.2427029
## depth...41  Gel040824_200um_2.csv             200 um         1 0.2040657
## depth...51  Gel040824_200um_2.csv             200 um         2 0.2093743
## depth...61  Gel040824_200um_2.csv             200 um         3 0.2081051
## depth...71  Gel040824_200um_3.csv             200 um         1 0.2263164
## depth...81  Gel040824_200um_3.csv             200 um         2 0.2147954
## depth...91  Gel040824_200um_3.csv             200 um         3 0.2215040
## depth...12 Gel040824_1000um_1.csv            1000 um         1 1.1332900
## depth...22 Gel040824_1000um_1.csv            1000 um         2 0.9957303
## depth...32 Gel040824_1000um_1.csv            1000 um         3 0.9178150
## depth...42 Gel040824_1000um_2.csv            1000 um         1 0.8172260
## depth...52 Gel040824_1000um_2.csv            1000 um         2 0.8410579
## depth...62 Gel040824_1000um_2.csv            1000 um         3 0.8079250
## depth...72 Gel040824_1000um_3.csv            1000 um         1 0.7755071
## depth...82 Gel040824_1000um_3.csv            1000 um         2 0.7735736
## depth...92 Gel040824_1000um_3.csv            1000 um         3 0.7711079
## depth...13  Gel040824_200um_1.csv             200 um         1 0.4842636
## depth...23  Gel040824_200um_1.csv             200 um         2 0.5132648
## depth...33  Gel040824_200um_1.csv             200 um         3 0.4619070
## depth...43  Gel040824_200um_2.csv             200 um         1 0.5319988
## depth...53  Gel040824_200um_2.csv             200 um         2 0.5372857
## depth...63  Gel040824_200um_2.csv             200 um         3 0.4678754
## depth...73  Gel040824_200um_3.csv             200 um         1 0.4546383
## depth...83  Gel040824_200um_3.csv             200 um         2 0.5200586
## depth...93  Gel040824_200um_3.csv             200 um         3 0.5592671
##                    Flux Breakpoint Oxygen_Surface Oxygen_breakpoint  condition
## depth...1  1.304912e-05  -222.7569        350.297           235.901      Light
## depth...2  1.276666e-05  -214.8453        342.694           230.712      Light
## depth...3  1.235544e-05  -239.9065        360.905           227.053      Light
## depth...4  1.126553e-05  -223.8684        327.998           231.279      Light
## depth...5  1.058643e-05  -217.2484        317.507           225.020      Light
## depth...6  1.080948e-05  -214.3318        318.232           223.906      Light
## depth...7  8.612406e-06  -234.9614        312.305           218.707      Light
## depth...8  1.004656e-05  -231.9015        328.562           218.860      Light
## depth...9  1.106507e-05  -229.3515        341.538           220.488      Light
## depth...11 4.644982e-06  -232.9567        265.988           215.260      Light
## depth...21 4.860979e-06  -229.2693        267.437           214.600      Light
## depth...31 5.440670e-06  -267.6632        276.688           217.204      Light
## depth...41 4.574541e-06  -271.7906        265.946           215.835      Light
## depth...51 4.693543e-06  -268.1471        266.835           215.400      Light
## depth...61 4.665093e-06  -262.7707        264.796           213.796      Light
## depth...71 5.073335e-06  -319.0819        282.498           216.533      Light
## depth...81 4.815069e-06  -236.1913        265.580           213.431      Light
## depth...91 4.965455e-06  -224.9881        260.208           216.236      Light
## depth...12 2.540496e-05  -210.4788        461.724           241.926 High_Light
## depth...22 2.232129e-05  -265.5822        493.305           248.847 High_Light
## depth...32 2.057466e-05  -313.5366        524.744           261.686 High_Light
## depth...42 1.831975e-05  -338.2794        517.052           234.581 High_Light
## depth...52 1.885399e-05  -315.0148        494.952           246.561 High_Light
## depth...62 1.811125e-05  -323.7080        489.418           246.379 High_Light
## depth...72 1.738454e-05  -280.7664        463.173           236.405 High_Light
## depth...82 1.734120e-05  -317.5293        474.739           243.014 High_Light
## depth...92 1.728592e-05  -322.6140        473.471           253.653 High_Light
## depth...13 1.085574e-05  -337.2228        396.543           229.495 High_Light
## depth...23 1.150586e-05  -364.5962        404.125           232.285 High_Light
## depth...33 1.035457e-05  -381.7309        411.104           233.866 High_Light
## depth...43 1.192582e-05  -328.6009        411.731           231.869 High_Light
## depth...53 1.204433e-05  -371.4499        420.229           234.575 High_Light
## depth...63 1.048836e-05  -431.0765        428.783           225.745 High_Light
## depth...73 1.019163e-05  -377.0699        397.521           222.018 High_Light
## depth...83 1.165815e-05  -369.5406        404.353           226.069 High_Light
## depth...93 1.253709e-05  -366.5890        417.810           230.045 High_Light
mean_flux <- Full_results_segmented_041324_High_Light %>%
  group_by(Scaffold_Thickness, condition) %>%
  summarize(mean_flux = mean(Flux))
## `summarise()` has grouped output by 'Scaffold_Thickness'. You can override
## using the `.groups` argument.
# Plotting the flux values for each thickness
flux_plot <- ggplot(Full_results_segmented_041324_High_Light, aes(x = condition, y = Flux, group = interaction(Scaffold_Thickness, condition), color = Scaffold_Thickness)) +
  geom_boxplot() + geom_point(position = position_dodge(width = 0.75)) +
  labs(x = "Light Condition",  y = "Flux") +
  ggtitle("Flux Comparison between Thicknesses and Light Condition,\nsame volumetric density (4/13/24)") +
  scale_color_manual(values = c("#4C413FFF", "#A6A0A0FF")) + theme_bw()

flux_plot

ggsave("Plots/Polymerized040824_Measured041324_Flux_plot_high_light.png", flux_plot, device = "png")
## Saving 7 x 5 in image
# Running ANOVA
anova_result <- aov(Flux ~ Scaffold_Thickness + condition + Scaffold_Thickness:condition, data = Full_results_segmented_041324_High_Light)
summary(anova_result)
##                              Df    Sum Sq   Mean Sq F value   Pr(>F)    
## Scaffold_Thickness            1 4.757e-10 4.757e-10 181.747 9.62e-15 ***
## condition                     1 4.904e-10 4.904e-10 187.372 6.33e-15 ***
## Scaffold_Thickness:condition  1 8.200e-12 8.200e-12   3.145   0.0857 .  
## Residuals                    32 8.380e-11 2.600e-12                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# What about for oxygen surface concentration?

mean_Oxygen_Surface <- Full_results_segmented_041324_High_Light %>%
  group_by(Scaffold_Thickness, condition) %>%
  summarize(mean_Oxygen_Surface = mean(Oxygen_Surface))
## `summarise()` has grouped output by 'Scaffold_Thickness'. You can override
## using the `.groups` argument.
# Plotting the surface O2 values for each thickness
Oxygen_surf <- ggplot(Full_results_segmented_041324_High_Light, aes(x = condition, y = Oxygen_Surface, group = interaction(Scaffold_Thickness, condition), color = Scaffold_Thickness)) +
  geom_boxplot() + geom_point(position = position_dodge(width = 0.75)) +
  labs(x = "Light Condition", y = "Oxygen Surface Concentration (µmol/L)") +
  ggtitle("Oxygen Surface Concentration (µmol/L) Comparison between\nThicknesses and Light Condition, same volumetric density (4/13/24)") +
  scale_color_manual(values = c("#4C413FFF", "#A6A0A0FF")) + theme_bw()

Oxygen_surf

ggsave("Plots/Polymerized040824_Measured041324_O2_plot_high_light.png", Oxygen_surf, device = "png")
## Saving 7 x 5 in image
# Running ANOVA
anova_result <- aov(Oxygen_Surface ~ Scaffold_Thickness + condition + Scaffold_Thickness:condition, data = Full_results_segmented_041324_High_Light)
summary(anova_result)
##                              Df Sum Sq Mean Sq F value   Pr(>F)    
## Scaffold_Thickness            1  45827   45827 197.181 3.13e-15 ***
## condition                     1 197842  197842 851.250  < 2e-16 ***
## Scaffold_Thickness:condition  1    376     376   1.617    0.213    
## Residuals                    32   7437     232                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# What about DBL Thickness?

mean_DBL_end <- Full_results_segmented_041324_High_Light %>%
  group_by(Scaffold_Thickness, condition) %>%
  summarize(mean_DBL_end = mean(Breakpoint))
## `summarise()` has grouped output by 'Scaffold_Thickness'. You can override
## using the `.groups` argument.
# Plotting the DBL Thickness for each scaffold thickness
DBL <- ggplot(Full_results_segmented_041324_High_Light, aes(x = condition, y = -Breakpoint, group = interaction(Scaffold_Thickness, condition), color = Scaffold_Thickness)) +
  geom_boxplot() + geom_point(position = position_dodge(width = 0.75)) +
  labs(x = "Light Condition",  y = "DBL Thickness (µm)") +
  ggtitle("DBL Thickness (µm) Comparison between Thicknesses and Light Condition,\nsame volumetric density (4/13/24)") +
  scale_color_manual(values = c("#4C413FFF", "#A6A0A0FF")) + theme_bw()

DBL

ggsave("Plots/Polymerized040824_Measured041324_DBL_plot_high_light.png", DBL, device = "png")
## Saving 7 x 5 in image
# Running ANOVA
anova_result <- aov(Breakpoint ~ Scaffold_Thickness + condition + Scaffold_Thickness:condition, data = Full_results_segmented_041324_High_Light)
summary(anova_result)
##                              Df Sum Sq Mean Sq F value   Pr(>F)    
## Scaffold_Thickness            1  23719   23719  27.728 9.17e-06 ***
## condition                     1  77781   77781  90.928 7.16e-11 ***
## Scaffold_Thickness:condition  1   3534    3534   4.131   0.0505 .  
## Residuals                    32  27373     855                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Plotting profiles with DBL

files <- list.files(path = "./Polymerized040824_Measured041324", pattern = "\\.csv$", full.names = TRUE)

plot_list <- list()

for (file in files) {
  file_name <- tools::file_path_sans_ext(basename(file))
  
  file_data <- read_csv(paste(file),show_col_types = FALSE) %>%
    mutate(Replicate = as.factor(Replicate)) %>%
    rename("Oxygen (µmol/L)" = `A FireSting Ch1 (µmol/L)`) %>% filter(Condition == "Light" | Condition == "Dark")
  
  # Extract the DBL depth for the current file
  dbl_depth <- Full_results_segmented_041324$Breakpoint[gsub("\\.csv$", "", Full_results_segmented_041324$File) == file_name]

  plot <- ggplot(data = file_data, aes(y = `Oxygen (µmol/L)`, x = `Depth (µm)`,
                                       color = `Condition`, shape = `Replicate`)) + 
      geom_point() + geom_line() +
      coord_flip() + scale_x_reverse() + xlim(0, -1000) + ylim(200, 400) + 
      theme_bw() + ggtitle(file_name) +
      annotate("text", x = -900, y = 395, label = paste("Start:", file_data$Time[1]), vjust = 1, hjust = 1, size = 3) +
      annotate("text", x = -850, y = 395, label = paste("End:", file_data$Time[nrow(file_data)]), vjust = 1, hjust = 1, size = 3) +
      geom_vline(xintercept = dbl_depth, linetype = "dashed") +  # Add DBL depth line
      theme(legend.position = "bottom") + scale_color_manual(values = c("#7EC8E3")) + theme_bw()

  #print(plot)
  
  plot_list[[file_name]] <- plot
}
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## New names:
## Scale for x is already present. Adding another scale for x, which will replace
## the existing scale.
## • `B No sensor` -> `B No sensor...7`
## • `B No sensor` -> `B No sensor...9`
arranged_plots <- arrangeGrob(grobs = plot_list, ncol = 3)
## Warning: Removed 72 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 72 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 72 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 72 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 72 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 72 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 33 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 33 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 33 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 33 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 33 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 33 rows containing missing values or values outside the scale range
## (`geom_line()`).
gridExtra::grid.arrange(arranged_plots)

ggsave("Plots/Polymerized040824_Measured041324_plots_DBL.png", arranged_plots, width = 16, height = 9, units = "in", device = "png")

Combine all three datasets

# Add a column indicating the date for each dataframe
Full_results_segmented_041024$date <- "041024"
Full_results_segmented_041024$polymerization_date <- "040824"
Full_results_segmented_041224$date <- "041224"
Full_results_segmented_041224$polymerization_date <- "041024"
Full_results_segmented_041324$date <- "041324"
Full_results_segmented_041324$polymerization_date <- "040824"

# ones polymerized on 040824 have the same volumetric density of algae (1000um scaffold should have 5x amount of algae as 200um scaffold)
# ones polymerized on 041024 have the same area density of algae (1000um scaffold should have same amount of algae as 200um scaffold)

# Combine the dataframes
Full_results_segmented <- rbind(Full_results_segmented_041024, 
                                Full_results_segmented_041224, 
                                Full_results_segmented_041324)
Full_results_segmented
##                               File Scaffold_Thickness Replicate      slope
## depth...1   Gel040824_1000um_4.csv            1000 um         1 0.41630000
## depth...2   Gel040824_1000um_4.csv            1000 um         2 0.29720343
## depth...3   Gel040824_1000um_4.csv            1000 um         3 0.34801086
## depth...4   Gel040824_1000um_5.csv            1000 um         1 0.31583000
## depth...5   Gel040824_1000um_5.csv            1000 um         2 0.22527071
## depth...6   Gel040824_1000um_5.csv            1000 um         3 0.34050200
## depth...7   Gel040824_1000um_6.csv            1000 um         1 0.30649200
## depth...8   Gel040824_1000um_6.csv            1000 um         2 0.26252400
## depth...9   Gel040824_1000um_6.csv            1000 um         3 0.27557400
## depth...11   Gel040824_200um_4.csv             200 um         1 0.10182714
## depth...21   Gel040824_200um_4.csv             200 um         2 0.12417314
## depth...31   Gel040824_200um_4.csv             200 um         3 0.11439429
## depth...41   Gel040824_200um_5.csv             200 um         1 0.09876400
## depth...51   Gel040824_200um_5.csv             200 um         2 0.09298171
## depth...61   Gel040824_200um_5.csv             200 um         3 0.10329143
## depth...71   Gel040824_200um_6.csv             200 um         1 0.09034171
## depth...81   Gel040824_200um_6.csv             200 um         2 0.06573738
## depth...91   Gel040824_200um_6.csv             200 um         3 0.08116286
## depth...12  Gel041024_1000um_1.csv            1000 um         1 0.18860467
## depth...22  Gel041024_1000um_1.csv            1000 um         2 0.18655467
## depth...32  Gel041024_1000um_1.csv            1000 um         3 0.19097533
## depth...42  Gel041024_1000um_2.csv            1000 um         1 0.15649833
## depth...52  Gel041024_1000um_2.csv            1000 um         2 0.16451467
## depth...62  Gel041024_1000um_2.csv            1000 um         3 0.18130633
## depth...72  Gel041024_1000um_3.csv            1000 um         1 0.25250143
## depth...82  Gel041024_1000um_3.csv            1000 um         2 0.19213233
## depth...92  Gel041024_1000um_3.csv            1000 um         3 0.19936433
## depth...111  Gel041024_200um_1.csv             200 um         1 0.20218800
## depth...211  Gel041024_200um_1.csv             200 um         2 0.21843400
## depth...311  Gel041024_200um_1.csv             200 um         3 0.24664400
## depth...411  Gel041024_200um_2.csv             200 um         1 0.15830190
## depth...511  Gel041024_200um_2.csv             200 um         2 0.17798833
## depth...611  Gel041024_200um_2.csv             200 um         3 0.18047152
## depth...711  Gel041024_200um_3.csv             200 um         1 0.12738000
## depth...811  Gel041024_200um_3.csv             200 um         2 0.12960857
## depth...911  Gel041024_200um_3.csv             200 um         3 0.13462500
## depth...13  Gel040824_1000um_1.csv            1000 um         1 0.58210800
## depth...23  Gel040824_1000um_1.csv            1000 um         2 0.56950800
## depth...33  Gel040824_1000um_1.csv            1000 um         3 0.55116400
## depth...43  Gel040824_1000um_2.csv            1000 um         1 0.50254400
## depth...53  Gel040824_1000um_2.csv            1000 um         2 0.47225000
## depth...63  Gel040824_1000um_2.csv            1000 um         3 0.48220000
## depth...73  Gel040824_1000um_3.csv            1000 um         1 0.38419086
## depth...83  Gel040824_1000um_3.csv            1000 um         2 0.44816686
## depth...93  Gel040824_1000um_3.csv            1000 um         3 0.49360171
## depth...112  Gel040824_200um_1.csv             200 um         1 0.20720800
## depth...212  Gel040824_200um_1.csv             200 um         2 0.21684343
## depth...312  Gel040824_200um_1.csv             200 um         3 0.24270286
## depth...412  Gel040824_200um_2.csv             200 um         1 0.20406571
## depth...512  Gel040824_200um_2.csv             200 um         2 0.20937429
## depth...612  Gel040824_200um_2.csv             200 um         3 0.20810514
## depth...712  Gel040824_200um_3.csv             200 um         1 0.22631643
## depth...812  Gel040824_200um_3.csv             200 um         2 0.21479543
## depth...912  Gel040824_200um_3.csv             200 um         3 0.22150400
##                     Flux Breakpoint Oxygen_Surface Oxygen_breakpoint   date
## depth...1   9.332197e-06  -186.4981        293.493           214.610 041024
## depth...2   6.662409e-06  -228.7611        285.615           212.969 041024
## depth...3   7.801359e-06  -226.2641        292.447           209.878 041024
## depth...4   7.079961e-06  -207.0086        281.456           221.030 041024
## depth...5   5.049894e-06  -276.5534        282.833           215.546 041024
## depth...6   7.633033e-06  -176.8002        283.175           216.541 041024
## depth...7   6.870631e-06  -162.7789        264.643           219.004 041024
## depth...8   5.885001e-06  -178.6104        269.245           217.059 041024
## depth...9   6.177542e-06  -207.1288        272.468           218.221 041024
## depth...11  2.282659e-06  -278.3708        235.138           206.914 041024
## depth...21  2.783589e-06  -269.4554        237.367           208.720 041024
## depth...31  2.564377e-06  -269.4545        235.725           208.342 041024
## depth...41  2.213993e-06  -236.2531        230.437           206.340 041024
## depth...51  2.084371e-06  -237.5620        229.684           207.066 041024
## depth...61  2.315484e-06  -266.7288        232.367           207.491 041024
## depth...71  2.025190e-06  -274.4906        231.559           209.808 041024
## depth...81  1.473635e-06  -340.0699        229.721           207.371 041024
## depth...91  1.819428e-06  -310.8676        230.814           207.816 041024
## depth...12  4.227951e-06  -417.4311        288.860           216.726 041224
## depth...22  4.181996e-06  -418.5536        289.131           216.760 041224
## depth...32  4.281094e-06  -389.4811        288.271           215.303 041224
## depth...42  3.508223e-06  -380.1215        273.166           212.490 041224
## depth...52  3.687925e-06  -387.7747        275.934           212.737 041224
## depth...62  4.064344e-06  -412.8095        280.751           211.682 041224
## depth...72  5.660325e-06  -321.0366        288.669           216.095 041224
## depth...82  4.307031e-06  -385.5178        287.524           212.723 041224
## depth...92  4.469150e-06  -385.2196        291.139           213.216 041224
## depth...111 4.532448e-06  -423.0096        284.485           207.667 041224
## depth...211 4.896635e-06  -386.3812        289.267           205.874 041224
## depth...311 5.529019e-06  -385.2487        294.029           202.154 041224
## depth...411 3.548654e-06  -373.7823        258.055           204.006 041224
## depth...511 3.989964e-06  -420.7698        272.079           203.448 041224
## depth...611 4.045630e-06  -438.4452        279.738           201.503 041224
## depth...711 2.855477e-06  -276.4187        234.767           197.122 041224
## depth...811 2.905435e-06  -277.3252        235.705           198.117 041224
## depth...911 3.017889e-06  -278.9906        236.974           197.286 041224
## depth...13  1.304912e-05  -222.7569        350.297           235.901 041324
## depth...23  1.276666e-05  -214.8453        342.694           230.712 041324
## depth...33  1.235544e-05  -239.9065        360.905           227.053 041324
## depth...43  1.126553e-05  -223.8684        327.998           231.279 041324
## depth...53  1.058643e-05  -217.2484        317.507           225.020 041324
## depth...63  1.080948e-05  -214.3318        318.232           223.906 041324
## depth...73  8.612406e-06  -234.9614        312.305           218.707 041324
## depth...83  1.004656e-05  -231.9015        328.562           218.860 041324
## depth...93  1.106507e-05  -229.3515        341.538           220.488 041324
## depth...112 4.644982e-06  -232.9567        265.988           215.260 041324
## depth...212 4.860979e-06  -229.2693        267.437           214.600 041324
## depth...312 5.440670e-06  -267.6632        276.688           217.204 041324
## depth...412 4.574541e-06  -271.7906        265.946           215.835 041324
## depth...512 4.693543e-06  -268.1471        266.835           215.400 041324
## depth...612 4.665093e-06  -262.7707        264.796           213.796 041324
## depth...712 5.073335e-06  -319.0819        282.498           216.533 041324
## depth...812 4.815069e-06  -236.1913        265.580           213.431 041324
## depth...912 4.965455e-06  -224.9881        260.208           216.236 041324
##             polymerization_date
## depth...1                040824
## depth...2                040824
## depth...3                040824
## depth...4                040824
## depth...5                040824
## depth...6                040824
## depth...7                040824
## depth...8                040824
## depth...9                040824
## depth...11               040824
## depth...21               040824
## depth...31               040824
## depth...41               040824
## depth...51               040824
## depth...61               040824
## depth...71               040824
## depth...81               040824
## depth...91               040824
## depth...12               041024
## depth...22               041024
## depth...32               041024
## depth...42               041024
## depth...52               041024
## depth...62               041024
## depth...72               041024
## depth...82               041024
## depth...92               041024
## depth...111              041024
## depth...211              041024
## depth...311              041024
## depth...411              041024
## depth...511              041024
## depth...611              041024
## depth...711              041024
## depth...811              041024
## depth...911              041024
## depth...13               040824
## depth...23               040824
## depth...33               040824
## depth...43               040824
## depth...53               040824
## depth...63               040824
## depth...73               040824
## depth...83               040824
## depth...93               040824
## depth...112              040824
## depth...212              040824
## depth...312              040824
## depth...412              040824
## depth...512              040824
## depth...612              040824
## depth...712              040824
## depth...812              040824
## depth...912              040824
# Define custom labels for facet
custom_labels <- c("040824" = "Polymerized 040824, Same Vol. Density",
                    "041024" = "Polymerized 041024, Same Areal Density")


mean_flux <- Full_results_segmented %>%
  group_by(Scaffold_Thickness, date, polymerization_date) %>%
  summarize(mean_flux = mean(Flux))
## `summarise()` has grouped output by 'Scaffold_Thickness', 'date'. You can
## override using the `.groups` argument.
# Plotting the flux values for each thickness
flux_plot <- ggplot(Full_results_segmented, aes(x = date, y = Flux, group = interaction(Scaffold_Thickness, date), color = Scaffold_Thickness)) +
  geom_boxplot() + geom_point(position = position_dodge(width = 0.75)) +  
  facet_grid( . ~polymerization_date, scales = "free_x",labeller = labeller(polymerization_date = custom_labels)) + 
  #geom_text(data = mean_flux, aes(label = paste("Mean Flux = \n", sprintf("%.2e", mean_flux)), y = mean_flux+.000002), vjust = 0.1, position = position_dodge(width = 0.75)) +
  labs(x = "Measurement Date", y = "Flux") +
  ggtitle("Flux Comparison between Thicknesses and Dates") + scale_color_manual(values = c("#4C413FFF", "#A6A0A0FF")) + theme_bw()

flux_plot

ggsave("Plots/Flux_plot.png", flux_plot, device = "png")
## Saving 7 x 5 in image
# Running ANOVA
anova_result <- aov(Flux ~ Scaffold_Thickness, data = Full_results_segmented)
summary(anova_result)
##                    Df    Sum Sq   Mean Sq F value   Pr(>F)    
## Scaffold_Thickness  1 1.958e-10 1.958e-10    34.9 2.69e-07 ***
## Residuals          52 2.917e-10 5.610e-12                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# What about for oxygen surface concentration?

mean_Oxygen_Surface <- Full_results_segmented %>%
  group_by(Scaffold_Thickness, date, polymerization_date) %>%
  summarize(mean_Oxygen_Surface = mean(Oxygen_Surface))
## `summarise()` has grouped output by 'Scaffold_Thickness', 'date'. You can
## override using the `.groups` argument.
# Plotting the surface O2 values for each thickness
O2_plot <- ggplot(Full_results_segmented, aes(x = date, y = Oxygen_Surface, group = interaction(Scaffold_Thickness, date), color = Scaffold_Thickness)) +
  geom_boxplot() + geom_point(position = position_dodge(width = 0.75)) + 
    facet_grid( . ~polymerization_date, scales = "free_x",labeller = labeller(polymerization_date = custom_labels)) + 
  #geom_text(data = mean_Oxygen_Surface, aes(label = paste("Mean Surface O2 = \n", round(mean_Oxygen_Surface, 1)), y = mean_Oxygen_Surface-25), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Measurement Date", y = "Oxygen Surface Concentration (µmol/L)") +
  ggtitle("Oxygen Surface Concentration (µmol/L) Comparison between Thicknesses and Dates") + scale_color_manual(values = c("#4C413FFF", "#A6A0A0FF")) + theme_bw()

O2_plot

ggsave("Plots/O2_plot.png", O2_plot, device = "png")
## Saving 7 x 5 in image
# Running ANOVA
anova_result <- aov(Oxygen_Surface ~ Scaffold_Thickness, data = Full_results_segmented)
summary(anova_result)
##                    Df Sum Sq Mean Sq F value   Pr(>F)    
## Scaffold_Thickness  1  26444   26444   44.42 1.69e-08 ***
## Residuals          52  30954     595                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# What about DBL Thickness?

mean_DBL_end <- Full_results_segmented %>%
  group_by(Scaffold_Thickness, date, polymerization_date) %>%
  summarize(mean_DBL_end = mean(Breakpoint))
## `summarise()` has grouped output by 'Scaffold_Thickness', 'date'. You can
## override using the `.groups` argument.
# Plotting the DBL Thickness for each scaffold thickness
DBL_plot <- ggplot(Full_results_segmented, aes(x = date, y = -Breakpoint, group = interaction(Scaffold_Thickness, date), color = Scaffold_Thickness)) +
  geom_boxplot() + geom_point(position = position_dodge(width = 0.75)) + 
    facet_grid( . ~polymerization_date, scales = "free_x",labeller = labeller(polymerization_date = custom_labels)) + 
  #geom_text(data = mean_DBL_end, aes(label = paste("Mean DBL Thickness = \n", round(-mean_DBL_end, 1)), y = -mean_DBL_end-65), vjust = 0.1, position = position_nudge(x = 0.0)) +
  labs(x = "Measurement Date", y = "DBL Thickness (µm)") +
  ggtitle("DBL Thickness (µm) Comparison between Thicknesses and Dates") + scale_color_manual(values = c("#4C413FFF", "#A6A0A0FF")) + theme_bw()

DBL_plot

ggsave("Plots/DBL_plot.png", DBL_plot, device = "png")
## Saving 7 x 5 in image
# Running ANOVA
anova_result <- aov(Breakpoint ~ Scaffold_Thickness, data = Full_results_segmented)
summary(anova_result)
##                    Df Sum Sq Mean Sq F value Pr(>F)
## Scaffold_Thickness  1   8537    8537   1.446  0.235
## Residuals          52 307096    5906